Memory
LRU cache for development and single-instance deployments.
@veloxts/cache provides a unified caching API with memory (LRU) and Redis drivers, cache tags, and distributed locks.
pnpm add @veloxts/cache
# For Redis (production)pnpm add ioredisimport { cachePlugin } from '@veloxts/cache';
app.register(cachePlugin({ driver: 'memory', config: { maxSize: 1000 },}));import { cachePlugin } from '@veloxts/cache';
app.register(cachePlugin({ driver: 'redis', config: { url: process.env.REDIS_URL, keyPrefix: 'myapp:cache:', },}));// Set value with TTLawait ctx.cache.put('user:123', user, '30m');
// Get valueconst user = await ctx.cache.get('user:123');
// Remember pattern (cache-aside)const data = await ctx.cache.remember('expensive:query', '1h', async () => { return await fetchExpensiveData();});
// Check existenceif (await ctx.cache.has('user:123')) { ... }
// Deleteawait ctx.cache.forget('user:123');'30s' // 30 seconds'5m' // 5 minutes'1h' // 1 hour'1d' // 1 day'1w' // 1 week3600 // 3600 seconds (number)// Set with tagsawait ctx.cache.tags(['users']).put('user:123', user);await ctx.cache.tags(['users']).put('user:456', user);
// Flush all tagged entriesawait ctx.cache.tags(['users']).flush();await ctx.cache.lockAndRun('payment:process', '30s', async () => { // Only one instance runs this at a time await processPayment();});await ctx.cache.increment('views:post:123');await ctx.cache.decrement('stock:item:456');Memory
LRU cache for development and single-instance deployments.
Redis
Shared cache for production with distributed locks.
| Feature | Memory | Redis |
|---|---|---|
| Shared across instances | No | Yes |
| Persists across restarts | No | Yes |
| Distributed locks | No | Yes |
| Memory management | Limited | External |
app.register(cachePlugin({ driver: 'redis', config: { url: process.env.REDIS_URL, keyPrefix: 'myapp:cache:', defaultTtl: '1h', },}));REDIS_URL=redis://user:password@your-redis-host:6379| Provider | Best For |
|---|---|
| Upstash | Serverless, pay-per-request |
| Redis Cloud | Managed Redis clusters |
| Railway | Simple Redis add-on |