Skip to content

Cache

@veloxts/cache provides a unified caching API.

Terminal window
pnpm add @veloxts/cache
import { cachePlugin } from '@veloxts/cache';
app.register(cachePlugin, {
driver: 'memory',
config: { maxSize: 1000 },
});
// Set value
await ctx.cache.put('user:123', user, '30m');
// Get value
const user = await ctx.cache.get('user:123');
// Remember pattern
const data = await ctx.cache.remember('key', '1h', async () => {
return fetchExpensiveData();
});
// Delete
await ctx.cache.forget('user:123');
cachePlugin({
driver: 'memory',
config: { maxSize: 1000 },
})
cachePlugin({
driver: 'redis',
config: { url: process.env.REDIS_URL },
})
// Set with tags
await ctx.cache.tags(['users']).put('user:123', user);
await ctx.cache.tags(['users']).put('user:456', user);
// Flush all tagged
await ctx.cache.tags(['users']).flush();