forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.service.ts
More file actions
108 lines (97 loc) · 2.58 KB
/
Copy pathredis.service.ts
File metadata and controls
108 lines (97 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Injectable, OnModuleInit, OnModuleDestroy, Logger } from '@nestjs/common';
import { createClient, RedisClientType } from 'redis';
@Injectable()
export class RedisService implements OnModuleInit, OnModuleDestroy {
private client: RedisClientType;
private readonly logger = new Logger(RedisService.name);
async onModuleInit() {
try {
this.client = createClient({
url: process.env.REDIS_URL || 'redis://localhost:6379',
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 1000),
},
});
this.client.on('error', (err) => {
this.logger.error('Redis client error:', err);
});
this.client.on('connect', () => {
this.logger.log('Redis client connected');
});
await this.client.connect();
} catch (error) {
this.logger.error('Failed to connect to Redis:', error);
}
}
async onModuleDestroy() {
if (this.client) {
await this.client.quit();
this.logger.log('Redis client disconnected');
}
}
/**
* Get value from cache
*/
async get(key: string): Promise<string | null> {
try {
return await this.client.get(key);
} catch (error) {
this.logger.error(`Failed to get cache key ${key}:`, error);
return null;
}
}
/**
* Set value in cache with TTL
*/
async set(key: string, value: string, ttlSeconds: number): Promise<boolean> {
try {
await this.client.setEx(key, ttlSeconds, value);
return true;
} catch (error) {
this.logger.error(`Failed to set cache key ${key}:`, error);
return false;
}
}
/**
* Delete value from cache
*/
async delete(key: string): Promise<boolean> {
try {
await this.client.del(key);
return true;
} catch (error) {
this.logger.error(`Failed to delete cache key ${key}:`, error);
return false;
}
}
/**
* Delete multiple keys
*/
async deletePattern(pattern: string): Promise<number> {
try {
let count = 0;
let cursor = 0;
do {
const reply = await this.client.scan(cursor, {
MATCH: pattern,
COUNT: 100,
});
cursor = reply.cursor;
if (reply.keys.length > 0) {
await this.client.del(reply.keys);
count += reply.keys.length;
}
} while (cursor !== 0);
return count;
} catch (error) {
this.logger.error(`Failed to delete pattern ${pattern}:`, error);
return 0;
}
}
/**
* Get Redis client for advanced operations
*/
getClient(): RedisClientType {
return this.client;
}
}