-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
37 lines (27 loc) · 819 Bytes
/
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
const redis = require('redis');
const { promisify } = require('util');
const client = redis.createClient();
client.on('connect', function() {
console.log('You are now connected to Redis Client');
});
const getAsync = promisify(client.get).bind(client);
const getHashAsync = promisify(client.hmget).bind(client);
const getAllHashAsync = promisify(client.hgetall).bind(client);
exports.addToCache = (key, value, exp) => {
if (exp) {
client.setex(key, exp, value);
} else {
client.set(key, value);
}
}
exports.getFromCache = (key) => getAsync(key);
exports.addToHash = (key, values) => {
client.hmset(key, values);
}
exports.getFromHash = (key, params) => {
if (params) {
return getHashAsync(key, params);
} else {
return getAllHashAsync(key);
}
}