change config redis db
This commit is contained in:
80
src/modules/cache/redis-cache.service.ts
vendored
80
src/modules/cache/redis-cache.service.ts
vendored
@@ -11,6 +11,7 @@ import Redis from 'ioredis';
|
||||
export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(RedisCacheService.name);
|
||||
private redis?: Redis;
|
||||
private isAvailable = false;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
@@ -30,51 +31,86 @@ export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
|
||||
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() {
|
||||
if (this.redis) {
|
||||
if (this.redis && this.isAvailable) {
|
||||
await this.redis.quit();
|
||||
}
|
||||
}
|
||||
|
||||
async getJson<T>(key: string): Promise<T | null> {
|
||||
if (!this.redis) {
|
||||
if (!this.redis || !this.isAvailable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const value = await this.redis.get(key);
|
||||
return value ? (JSON.parse(value) as T) : null;
|
||||
try {
|
||||
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) {
|
||||
if (!this.redis) {
|
||||
if (!this.redis || !this.isAvailable) {
|
||||
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) {
|
||||
if (!this.redis) {
|
||||
if (!this.redis || !this.isAvailable) {
|
||||
return;
|
||||
}
|
||||
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [nextCursor, keys] = await this.redis.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
pattern,
|
||||
'COUNT',
|
||||
200,
|
||||
);
|
||||
cursor = nextCursor;
|
||||
try {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [nextCursor, keys] = await this.redis.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
pattern,
|
||||
'COUNT',
|
||||
200,
|
||||
);
|
||||
cursor = nextCursor;
|
||||
|
||||
if (keys.length > 0) {
|
||||
await this.redis.del(...keys);
|
||||
}
|
||||
} while (cursor !== '0');
|
||||
if (keys.length > 0) {
|
||||
await this.redis.del(...keys);
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user