-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-memcached-store.js
38 lines (27 loc) · 1.2 KB
/
sync-memcached-store.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
module.exports = MemcachedStore;
var Memcached = require('memcached'), Sync = require('syncho');
function MemcachedStore (options) {
if (!(this instanceof MemcachedStore))
return new MemcachedStore(options);
if (typeof options === 'string') options = {uri: options};
if (! options || ! options.uri)
throw new Error("Must pass a memcached connection string or an option object with at least a `uri` property.");
this.lifetime = options.maxAge && options.maxAge >= 1000 ? parseInt(options.maxAge/1000) : 60;
// TODO: fix timeout connection not firing!!!
this.mc = new MemcachedStore.Memcached(options.uri, {namespace: options.ns || '', timeout: options.timeout || 1000});
}
MemcachedStore.Memcached = Memcached;
MemcachedStore.prototype.set = function (key, value) {
return this.mc.set.sync(this.mc, key, value, this.lifetime);
};
MemcachedStore.prototype.get = function (key) {
return this.mc.get.sync(this.mc, key) || void 0;
};
MemcachedStore.prototype.del = function (key) {
return this.mc.del.sync(this.mc, key);
};
MemcachedStore.prototype.has = function (key) {
return true;
};
MemcachedStore.prototype.peek = MemcachedStore.prototype.get;
MemcachedStore.prototype.reset = function () {};