-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathredis.service.ts
More file actions
88 lines (73 loc) · 2.99 KB
/
Copy pathredis.service.ts
File metadata and controls
88 lines (73 loc) · 2.99 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
import { Inject, Injectable, Logger } from '@nestjs/common';
import Redis from 'ioredis';
@Injectable()
export class RedisService {
private readonly logger = new Logger(RedisService.name);
constructor(@Inject('REDIS_CLIENT') private readonly redis: Redis) {}
async ping(): Promise<string> {
try {
return await this.redis.ping();
} catch (error) {
this.logger.warn(`Redis ping unavailable: ${error instanceof Error ? error.message : 'unknown error'}`);
return 'UNAVAILABLE';
}
}
async getGroupBalanceCache(groupId: string): Promise<string | null> {
return this.safeGet(`group:${groupId}:balances`);
}
async setGroupBalanceCache(groupId: string, payload: string, ttlSeconds = 120): Promise<void> {
await this.safeSet(`group:${groupId}:balances`, payload, ttlSeconds);
}
async getGroupMembersCache(groupId: string): Promise<string | null> {
return this.safeGet(`group:${groupId}:members`);
}
async setGroupMembersCache(groupId: string, payload: string, ttlSeconds = 120): Promise<void> {
await this.safeSet(`group:${groupId}:members`, payload, ttlSeconds);
}
async getGroupExpenseSummaryCache(groupId: string): Promise<string | null> {
return this.safeGet(`group:${groupId}:expense_summary`);
}
async setGroupExpenseSummaryCache(groupId: string, payload: string, ttlSeconds = 120): Promise<void> {
await this.safeSet(`group:${groupId}:expense_summary`, payload, ttlSeconds);
}
async getGroupSummaryCache(groupId: string): Promise<string | null> {
return this.safeGet(`group:${groupId}:summary`);
}
async setGroupSummaryCache(groupId: string, payload: string, ttlSeconds = 120): Promise<void> {
await this.safeSet(`group:${groupId}:summary`, payload, ttlSeconds);
}
async invalidateGroupCache(groupId: string): Promise<void> {
try {
await this.redis.del(
`group:${groupId}:balances`,
`group:${groupId}:members`,
`group:${groupId}:expense_summary`,
`group:${groupId}:summary`,
);
} catch (error) {
this.logger.warn(`Redis invalidate skipped: ${error instanceof Error ? error.message : 'unknown error'}`);
}
}
async invalidateUserDashboardCache(userId: string): Promise<void> {
try {
await this.redis.del(`user:${userId}:dashboard`);
} catch (error) {
this.logger.warn(`Redis invalidate skipped: ${error instanceof Error ? error.message : 'unknown error'}`);
}
}
private async safeGet(key: string): Promise<string | null> {
try {
return await this.redis.get(key);
} catch (error) {
this.logger.warn(`Redis get skipped for ${key}: ${error instanceof Error ? error.message : 'unknown error'}`);
return null;
}
}
private async safeSet(key: string, payload: string, ttlSeconds: number): Promise<void> {
try {
await this.redis.set(key, payload, 'EX', ttlSeconds);
} catch (error) {
this.logger.warn(`Redis set skipped for ${key}: ${error instanceof Error ? error.message : 'unknown error'}`);
}
}
}