This commit is contained in:
2025-12-18 00:19:01 +03:30
parent 4319be7a04
commit 32ad4f58d5
6 changed files with 116 additions and 144 deletions
+13 -15
View File
@@ -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 }),
],
};
},
};
}
}