-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredis-component-cache.js
More file actions
46 lines (37 loc) · 951 Bytes
/
redis-component-cache.js
File metadata and controls
46 lines (37 loc) · 951 Bytes
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
'use strict'
module.exports = RedisComponentCache
var RedisClient = require('ioredis');
const hasSymbol = typeof Symbol === 'function'
var makeSymbol
if (hasSymbol) {
makeSymbol = function (key) {
return Symbol.for(key)
}
} else {
makeSymbol = function (key) {
return '_' + key
}
}
var REDIS = makeSymbol('redis')
var TTL = makeSymbol('ttl')
function RedisComponentCache (options) {
if (!(this instanceof RedisComponentCache)) {
return new RedisComponentCache(options)
}
this[REDIS] = new RedisClient(options.redis)
this[TTL] = options.ttl || 3600
}
RedisComponentCache.prototype.set = function (key, val) {
this[REDIS].set(key, val)
this[REDIS].expire(key, this[TTL])
}
RedisComponentCache.prototype.get = function (key, cb) {
this[REDIS].get(key).then((res) => {
cb(res)
})
}
RedisComponentCache.prototype.has = function (key, cb) {
this[REDIS].exists(key).then((res) => {
cb(1 === res)
})
}