-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
41 lines (32 loc) · 1005 Bytes
/
index.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
// we use redback as it has some higher level functionality!
var redback = require("redback")
function CacherRedis(port, host, opts) {
if (port instanceof redback.Client) {
this.client = redback.use(port)
}
opts = opts || {}
this.client = this.client || new redback.createClient(port, host, opts)
opts.namespace = opts.namespace || "cacher"
this.cache = this.client.createCache(opts.namespace)
}
CacherRedis.prototype.get = function(key, cb) {
this.cache.get(key, function(err, obj) {
if (err) return cb(err)
try {
obj = JSON.parse(obj)
} catch (e) {
} finally {
cb(null, obj)
}
})
}
CacherRedis.prototype.set = function(key, cacheObject, ttl, cb) {
cb = cb || function() {}
if (typeof cacheObject == 'object') cacheObject = JSON.stringify(cacheObject)
this.cache.set(key, cacheObject, ttl, cb)
}
CacherRedis.prototype.invalidate = function(key, cb) {
cb = cb || function() {}
this.cache.flush(key, cb)
}
module.exports = CacherRedis