You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
6 years ago
|
var redis = require("redis");
|
||
|
var bluebird = require("bluebird");
|
||
|
|
||
|
var config = require("./config.js");
|
||
|
|
||
|
var redisClient = null;
|
||
|
if (config.redisUrl) {
|
||
|
bluebird.promisifyAll(redis.RedisClient.prototype);
|
||
|
|
||
|
redisClient = redis.createClient({url:config.redisUrl});
|
||
|
}
|
||
|
|
||
|
function onCacheEvent(cacheType, hitOrMiss, cacheKey) {
|
||
|
//console.log(`cache.${cacheType}.${hitOrMiss}: ${cacheKey}`);
|
||
|
}
|
||
|
|
||
|
var redisCache = {
|
||
|
get:function(key) {
|
||
|
return new Promise(function(resolve, reject) {
|
||
|
redisClient.getAsync(key).then(function(result) {
|
||
|
if (result == null) {
|
||
|
onCacheEvent("redis", "miss", key);
|
||
|
|
||
|
resolve(null);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
onCacheEvent("redis", "hit", key);
|
||
|
|
||
|
resolve(JSON.parse(result));
|
||
|
|
||
|
}).catch(function(err) {
|
||
|
console.log(`Error 328rhwefghsdgsdss: ${err}`);
|
||
|
|
||
|
reject(err);
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
set:function(key, obj, maxAgeMillis) {
|
||
|
redisClient.set(key, JSON.stringify(obj), "PX", maxAgeMillis);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
active: (redisClient != null),
|
||
|
get: redisCache.get,
|
||
|
set: redisCache.set
|
||
|
}
|