-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
60 lines (52 loc) · 1.42 KB
/
cache.js
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
const redis = require('redis');
const redisClient = redis.createClient({
host: '172.28.0.2',
port: 6379
});
// Properly connect Redis client
redisClient.connect().catch(console.error);
// Connect Redis client
redisClient.on('connect', () => {
console.log('Connected to Redis');
});
// Error handling for Redis
redisClient.on('error', (err) => {
console.error('Redis error:', err);
});
// Fetch data from cache
async function getFromCache(key) {
try {
const cachedData = await redisClient.get(key);
return cachedData ? JSON.parse(cachedData) : null;
} catch (error) {
console.error('Error fetching from Redis cache:', error);
throw error;
}
}
// Set data to cache with expiration
async function setToCache(key, data, ttl = 3600) {
try {
await redisClient.setEx(key, ttl, JSON.stringify(data));
} catch (error) {
console.error('Error setting to Redis cache:', error);
throw error;
}
}
// Invalidate cache by keys
async function invalidateCache(pattern) {
try {
const keys = await redisClient.keys(pattern);
if (keys.length > 0) {
await redisClient.del(keys);
console.log('Cache invalidated for keys:', keys);
}
} catch (error) {
console.error('Error invalidating Redis cache:', error);
throw error;
}
}
module.exports = {
getFromCache,
setToCache,
invalidateCache
};