change config redis db

This commit is contained in:
2026-03-29 17:40:32 +03:00
parent dcd170b595
commit d59041ff4c

View File

@@ -11,6 +11,7 @@ import Redis from 'ioredis';
export class RedisCacheService implements OnModuleInit, OnModuleDestroy { export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(RedisCacheService.name); private readonly logger = new Logger(RedisCacheService.name);
private redis?: Redis; private redis?: Redis;
private isAvailable = false;
constructor(private readonly configService: ConfigService) {} constructor(private readonly configService: ConfigService) {}
@@ -30,51 +31,86 @@ export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
this.logger.error(`Redis error: ${error.message}`); this.logger.error(`Redis error: ${error.message}`);
}); });
await this.redis.connect(); try {
await this.redis.connect();
this.isAvailable = true;
this.logger.log('Redis cache connected.');
} catch (error) {
this.logger.error(
`Redis connection failed. Caching disabled. ${(error as Error).message}`,
);
this.isAvailable = false;
try {
this.redis.disconnect(false);
} catch {
// Ignore cleanup errors when Redis bootstrap fails.
}
}
} }
async onModuleDestroy() { async onModuleDestroy() {
if (this.redis) { if (this.redis && this.isAvailable) {
await this.redis.quit(); await this.redis.quit();
} }
} }
async getJson<T>(key: string): Promise<T | null> { async getJson<T>(key: string): Promise<T | null> {
if (!this.redis) { if (!this.redis || !this.isAvailable) {
return null; return null;
} }
const value = await this.redis.get(key); try {
return value ? (JSON.parse(value) as T) : null; const value = await this.redis.get(key);
return value ? (JSON.parse(value) as T) : null;
} catch (error) {
this.handleRuntimeFailure(error);
return null;
}
} }
async setJson(key: string, value: unknown, ttlSeconds: number) { async setJson(key: string, value: unknown, ttlSeconds: number) {
if (!this.redis) { if (!this.redis || !this.isAvailable) {
return; return;
} }
await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds); try {
await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds);
} catch (error) {
this.handleRuntimeFailure(error);
}
} }
async deleteByPattern(pattern: string) { async deleteByPattern(pattern: string) {
if (!this.redis) { if (!this.redis || !this.isAvailable) {
return; return;
} }
let cursor = '0'; try {
do { let cursor = '0';
const [nextCursor, keys] = await this.redis.scan( do {
cursor, const [nextCursor, keys] = await this.redis.scan(
'MATCH', cursor,
pattern, 'MATCH',
'COUNT', pattern,
200, 'COUNT',
); 200,
cursor = nextCursor; );
cursor = nextCursor;
if (keys.length > 0) { if (keys.length > 0) {
await this.redis.del(...keys); await this.redis.del(...keys);
} }
} while (cursor !== '0'); } while (cursor !== '0');
} catch (error) {
this.handleRuntimeFailure(error);
}
}
private handleRuntimeFailure(error: unknown) {
this.logger.error(
`Redis became unavailable. Caching disabled. ${(error as Error).message}`,
);
this.isAvailable = false;
} }
} }