-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.ts
More file actions
176 lines (152 loc) · 4.39 KB
/
cache.ts
File metadata and controls
176 lines (152 loc) · 4.39 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { getRedisClient, isRedisConnected, DEFAULT_TTL } from './redis-client.js';
export interface CacheOptions {
ttl?: number; // Time to live in seconds
}
/**
* Get a value from Redis cache
*/
export async function getCacheValue<T>(key: string): Promise<T | null> {
if (!isRedisConnected()) {
return null;
}
try {
const client = await getRedisClient();
const value = await client.get(key);
if (!value) {
return null;
}
return JSON.parse(value) as T;
} catch (error) {
console.error(`Error retrieving cache for key ${key}:`, error);
return null;
}
}
/**
* Set a value in Redis cache with optional TTL
*/
export async function setCacheValue(
key: string,
value: any,
options: CacheOptions = {}
): Promise<boolean> {
if (!isRedisConnected()) {
return false;
}
try {
const client = await getRedisClient();
const ttl = options.ttl || DEFAULT_TTL.USER_DATA;
await client.setEx(key, ttl, JSON.stringify(value));
return true;
} catch (error) {
console.error(`Error setting cache for key ${key}:`, error);
return false;
}
}
/**
* Delete a value from Redis cache
*/
export async function deleteCacheValue(key: string): Promise<boolean> {
if (!isRedisConnected()) {
return false;
}
try {
const client = await getRedisClient();
await client.del(key);
return true;
} catch (error) {
console.error(`Error deleting cache for key ${key}:`, error);
return false;
}
}
/**
* Delete multiple values from Redis cache
*/
export async function deleteCacheValues(keys: string[]): Promise<boolean> {
if (!isRedisConnected() || keys.length === 0) {
return false;
}
try {
const client = await getRedisClient();
await client.del(keys);
return true;
} catch (error) {
console.error(`Error deleting cache values:`, error);
return false;
}
}
/**
* Clear all cache entries for a specific user (pattern-based deletion)
*/
export async function clearUserCache(username: string): Promise<boolean> {
if (!isRedisConnected()) {
return false;
}
try {
const client = await getRedisClient();
const keys = await client.keys(`*:${username}:*`);
if (keys.length > 0) {
await client.del(keys);
}
return true;
} catch (error) {
console.error(`Error clearing cache for user ${username}:`, error);
return false;
}
}
/**
* Get or set cache value with a fallback function
* Useful for cache-aside pattern
*/
export async function getCacheOrCompute<T>(
key: string,
computeFn: () => Promise<T>,
options: CacheOptions = {}
): Promise<T> {
try {
// Try to get from cache first
const cached = await getCacheValue<T>(key);
if (cached !== null) {
return cached;
}
// If not in cache, compute the value
const value = await computeFn();
// Store in cache for future requests
await setCacheValue(key, value, options);
return value;
} catch (error) {
console.error(`Error in getCacheOrCompute for key ${key}:`, error);
// If all else fails, return the computed value
return computeFn();
}
}
/**
* Increment a counter in Redis (useful for visitor counts, etc.)
*/
export async function incrementCounter(key: string, amount: number = 1): Promise<number> {
if (!isRedisConnected()) {
return 0;
}
try {
const client = await getRedisClient();
return await client.incrBy(key, amount);
} catch (error) {
console.error(`Error incrementing counter ${key}:`, error);
return 0;
}
}
/**
* Set expiration on an existing key
*/
export async function setExpiration(key: string, ttl: number): Promise<boolean> {
if (!isRedisConnected()) {
return false;
}
try {
const client = await getRedisClient();
const result = await client.expire(key, ttl);
return result === true;
} catch (error) {
console.error(`Error setting expiration for key ${key}:`, error);
return false;
}
}