-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
45 lines (38 loc) · 1.54 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
const { createClient } = require('redis');
module.exports = function (connectionStr) {
if (connectionStr && connectionStr.indexOf('redis://') === 0) {
const PREFIX = 'video-recorder:';
let client = createClient({
url: connectionStr
});
client.on('error', (err) => console.log('Redis error', err));
client.connect();
process.on("SIGINT", () => { client.quit() } );
process.on("SIGTERM", () =>{ client.quit() } );
return {
getCache: async function(key) {
let value = await client.get(PREFIX + key);
if (value == 'nil') {
return null;
}
client.expire(PREFIX + key, 60 * 60 * 3, 'GT'); // refresh it
return value ? JSON.parse(value) : null;
},
setCache: async function(key, value) {
// Assume after 24 hours nothing is using it
return await client.setEx(PREFIX + key, 60 * 60 * 3, JSON.stringify(value));
},
has: async function(key) {
let has = await client.exists(PREFIX + key);
return has > 0;
},
}
} else {
const memory = new Map();
return {
getCache: (key) => Promise.resolve(memory.get(key) || null),
setCache: (key, value) => Promise.resolve(memory.set(key, value) && true),
has: (key) => Promise.resolve(memory.has(key)),
}
}
};