@cache-utils/decorators
v0.0.2
Published
@Cacheable / @CacheEvict method decorators for @cache-utils/core, usable with any DI framework or none.
Maintainers
Readme
@cache-utils/decorators
@Cacheable / @CacheEvict method decorators for @cache-utils/core, usable with any DI
framework or none.
Install
npm install @cache-utils/decorators @cache-utils/coreUsage
Configure a CacheManager once at startup, then decorate methods:
import { CacheManager } from "@cache-utils/core";
import { configureCacheManager, Cacheable, CacheEvict } from "@cache-utils/decorators";
configureCacheManager(new CacheManager());
class ProductService {
@Cacheable({ keyFrom: (id: string) => `product:${id}`, ttlSeconds: 300 })
async getProduct(id: string) {
return db.products.findOne(id);
}
@CacheEvict({ keyFrom: (id: string) => `product:${id}` })
async updateProduct(id: string, data: Partial<Product>) {
return db.products.update(id, data);
}
}key/keyFrom(...args)— static or per-call cache key. Falls back to the method name if neither is given.ttlSeconds/ttlSecondsFrom(...args)(@Cacheableonly) — static or per-call TTL.
Using NestJS? @cache-utils/nestjs wires configureCacheManager into a
forRoot() module for you.
License
MIT
