This commit is contained in:
2025-11-09 11:25:26 +03:30
commit 06f814b331
62 changed files with 31547 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
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
export function cacheConfig(): CacheModuleAsyncOptions {
// 2. Create a logger instance
const logger = new Logger('CacheConfig');
return {
isGlobal: true,
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const ttl = configService.get<number>('CACHE_TTL', 120);
const namespace = configService.get<string>('CACHE_NAMESPACE', 'app-cache');
const redisUri = configService.getOrThrow<string>('REDIS_URI');
// 3. Create the KeyvRedis store first
const keyvRedisStore = new KeyvRedis(redisUri);
// 4. Attach event listeners
keyvRedisStore.client.on('connect', () => {
logger.log('✅ Redis connected successfully.');
});
keyvRedisStore.client.on('error', (error) => {
logger.error('❌ Redis connection error:', error);
});
// 5. Pass the store to Keyv
const store = new Keyv({
store: keyvRedisStore, // Use the instance
namespace,
ttl: ttl * 1000,
});
return {
store,
ttl,
};
},
};
}