cache
This commit is contained in:
+13
-15
@@ -1,29 +1,27 @@
|
||||
import { Keyv } from 'keyv';
|
||||
import KeyvRedis from '@keyv/redis';
|
||||
import Keyv from 'keyv';
|
||||
import type { CacheModuleAsyncOptions } from '@nestjs/cache-manager';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Logger } from '@nestjs/common'; // 1. Import Logger
|
||||
import { CacheableMemory } from 'cacheable';
|
||||
|
||||
export function cacheConfig(): CacheModuleAsyncOptions {
|
||||
return {
|
||||
isGlobal: true,
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => {
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
const redisUri = configService.get('REDIS_URI');
|
||||
const namespace = configService.get('CACHE_NAMESPACE', 'app-cache');
|
||||
const ttl = configService.get<number>('CACHE_TTL', 3600);
|
||||
const namespace = configService.get<string>('CACHE_NAMESPACE', 'app-cache');
|
||||
|
||||
const redisUri = configService.getOrThrow<string>('REDIS_URI');
|
||||
|
||||
const store = new Keyv({
|
||||
store: new KeyvRedis(redisUri),
|
||||
namespace,
|
||||
});
|
||||
|
||||
return {
|
||||
store,
|
||||
ttl, // cache-manager handles TTL
|
||||
stores: [
|
||||
new Keyv({
|
||||
store: new CacheableMemory({ ttl: ttl*1000, lruSize: 5000 }),
|
||||
namespace: namespace
|
||||
}),
|
||||
new KeyvRedis(redisUri, { namespace: namespace }),
|
||||
],
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user