change config redis db
This commit is contained in:
44
src/modules/cache/redis-cache.service.ts
vendored
44
src/modules/cache/redis-cache.service.ts
vendored
@@ -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,37 +31,62 @@ export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
|
|||||||
this.logger.error(`Redis error: ${error.message}`);
|
this.logger.error(`Redis error: ${error.message}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await this.redis.connect();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const value = await this.redis.get(key);
|
const value = await this.redis.get(key);
|
||||||
return value ? (JSON.parse(value) as T) : null;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
let cursor = '0';
|
let cursor = '0';
|
||||||
do {
|
do {
|
||||||
const [nextCursor, keys] = await this.redis.scan(
|
const [nextCursor, keys] = await this.redis.scan(
|
||||||
@@ -76,5 +102,15 @@ export class RedisCacheService implements OnModuleInit, OnModuleDestroy {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user