@lowerdeck/cache
Multi-layer caching with Redis and in-memory LRU cache. Supports tag-based invalidation, dynamic TTL, and local-only caching for optimal performance.
Installation
npm install @lowerdeck/cache
yarn add @lowerdeck/cache
bun add @lowerdeck/cache
pnpm add @lowerdeck/cache
Usage
Redis-backed cache
import { createCachedFunction } from '@lowerdeck/cache';
const getUserProfile = createCachedFunction({
name: 'user-profile',
redisUrl: 'redis://localhost:6379',
getHash: (userId: string) => userId,
provider: async (userId, { setTTL }) => {
const user = await database.getUser(userId);
if (user.isPremium) {
setTTL(3600);
}
return user;
},
ttlSeconds: 300,
getTags: (user, userId) => [`user:${userId}`, `role:${user.role}`]
});
const user = await getUserProfile('123');
await getUserProfile.clear('123');
await getUserProfile.clearByTag('role:admin');
await getUserProfile.clearAndWait('123');
await getUserProfile.clearByTagAndWait('role:admin');
Local-only cache
import { createLocallyCachedFunction } from '@lowerdeck/cache';
const computeHash = createLocallyCachedFunction({
getHash: (input: string) => input,
provider: async (input) => {
return expensiveHashComputation(input);
},
ttlSeconds: 60
});
const hash1 = await computeHash('data');
const hash2 = await computeHash('data');
await computeHash.clear('data');
await computeHash.clearAndWait('data');
License
This project is licensed under the Apache License 2.0.