@tc-libs/app-cache
v3.14.0
Published
Astrazione cache per NestJS basata su `@nestjs/cache-manager`. Supporta due modalita:
Downloads
826
Readme
@tc-libs/app-cache
Astrazione cache per NestJS basata su @nestjs/cache-manager. Supporta due modalita:
- cache in-memory quando
enabledefalse - cache Redis quando
enabledetrue
Il package viene usato soprattutto dai repository Mongo del monorepo per cacheare query e count.
Registrazione
AppCacheModule.register(
true,
{
enabled: true,
ttl: 60,
server: {
host: '127.0.0.1',
port: 6379,
},
},
true,
);Versione async:
AppCacheModule.registerAsync(
true,
{
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
enabled: true,
ttl: 60,
server: {
host: config.getOrThrow<string>('REDIS_HOST'),
port: config.getOrThrow<number>('REDIS_PORT'),
},
}),
},
true,
);Servizio esportato
AppCacheService espone i metodi usati dal resto del monorepo:
getCachedDataByQuery(functionName, find, options)getCachedDataByKey(key)saveToCache(key, data, options)
Lato repository il pattern tipico e:
const { data, key } = await this.cache.getCachedDataByQuery(
'findOne',
find,
options,
);
if (data) return data;
this.cache.saveToCache(key, result, options);Note operative
- La chiave cache viene derivata da
functionName, filtro e opzioni tramite hash SHA1 Base64. - La cache viene usata solo se il modulo e abilitato e l'operazione passa
options.caching.enabled. - Se vuoi una chiave custom puoi passare
options.caching.key.
Export pubblici
import {
AppCacheModule,
AppCacheService,
ICachingOptions,
} from '@tc-libs/app-cache';Sviluppo
nx build app-cache
nx test app-cache