@cache-utils/nestjs
v0.0.2
Published
NestJS module wiring @cache-utils/core into Nest's DI, with @Cacheable/@CacheEvict decorator support.
Maintainers
Readme
@cache-utils/nestjs
NestJS module wiring @cache-utils/core into Nest's DI, with
@Cacheable/@CacheEvict decorator support out of the box.
forRoot() builds one CacheManager and both registers it as a Nest
provider (CACHE_MANAGER, for manual get/set/wrap/lock use) and points
@Cacheable/@CacheEvict at it — so the two ways of using this package
share the same manager instance.
Install
npm install @cache-utils/nestjs @cache-utils/core @cache-utils/decoratorsUsage
import { Module, Inject } from "@nestjs/common";
import { CacheUtilsModule, CACHE_MANAGER, CacheManager, Cacheable } from "@cache-utils/nestjs";
import { MemoryAdapter } from "@cache-utils/core"; // or RedisAdapter
@Module({
imports: [
CacheUtilsModule.forRoot({
adapter: new MemoryAdapter(),
isGlobal: true, // optional, defaults to false
}),
],
})
export class AppModule {}
@Injectable()
class ProductService {
constructor(@Inject(CACHE_MANAGER) private readonly cache: CacheManager) {}
@Cacheable({ keyFrom: (id: string) => `product:${id}`, ttlSeconds: 300 })
async getProduct(id: string) {
return this.db.products.findOne(id);
}
}License
MIT
