Redis-backed cache module for NestJS with versioned keys and optional metrics
npm install nest-cache-layer
npm i nest-cache-layer
`
Usage
`ts
import { AppCacheModule } from "nest-cache-layer";
@Module({
imports: [
AppCacheModule.forRoot({
redisUrl: process.env.REDIS_URL,
defaultTtlSeconds: 60,
keyPrefix: "reporting",
metrics: {
recordCacheHit: (bucket) => console.log("hit", bucket),
recordCacheMiss: (bucket) => console.log("miss", bucket),
},
}),
],
})
export class AppModule {}
`Example module + controller
`ts
import { Module } from "@nestjs/common";
import { AppCacheModule, ExampleModule } from "nest-cache-layer";@Module({
imports: [
AppCacheModule.forRoot({
redisUrl: process.env.REDIS_URL,
defaultTtlSeconds: 60,
keyPrefix: "example",
}),
ExampleModule,
],
})
export class AppModule {}
`Example endpoints:
-
GET /example/greeting?name=you
- POST /example/greeting/bumpExample buckets/TTLs
`ts
export const MyCacheBuckets = {
ORDERS_AGG: "orders-agg",
} as const;
export type MyCacheBucket =
(typeof MyCacheBuckets)[keyof typeof MyCacheBuckets];
export const MyCacheTtlSeconds = {
ORDERS_LIST: 30,
} as const;
`
API
- AppCacheService.getOrSet(bucket, suffix, ttlSeconds, loader)
- AppCacheService.bumpBucket(bucket) / bumpBuckets(buckets)
- CacheBucket type and CacheTtlMap` helper type are exported