unpkg/server/middleware/utils/createCache.js

30 lines
557 B
JavaScript
Raw Normal View History

2017-08-19 06:09:29 +00:00
const db = require('../../RedisClient')
function createCache(keyPrefix) {
function createKey(key) {
return keyPrefix + '-' + key
}
function set(key, value, expiry, callback) {
db.setex(createKey(key), expiry, JSON.stringify(value), callback)
}
function get(key, callback) {
db.get(createKey(key), function (error, value) {
callback(error, value && JSON.parse(value))
})
}
function del(key, callback) {
db.del(createKey(key), callback)
}
return {
set,
get,
del
}
}
module.exports = createCache