-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached.js
88 lines (67 loc) · 1.83 KB
/
memcached.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var getenv = require('getenv');
var Memcached = require('memcached');
var thunkify = require('thunkify');
var MEMCACHED_PORT = getenv('MEMCACHED_PORT_6379_TCP_PORT', '11211');
var MEMCACHED_HOST = getenv('MEMCACHED_PORT_6379_TCP_ADDR', '127.0.0.1');
var ONE_WEEK = 60 * 60 * 24 * 7;
console.log('MEMCACHED CONNECTION: ' + MEMCACHED_HOST + ':' + MEMCACHED_PORT);
var store = new Memcached(MEMCACHED_HOST + ':' + MEMCACHED_PORT);
store.$del = thunkify(store.del);
store.$get = thunkify(store.get);
store.$set = thunkify(store.set);
module.exports = exports = {
del : function* del(key) {
return yield store.$del(key);
},
get : function* get(key) {
return yield store.$get(key);
},
smembers : function* smembers(key) {
var list = yield store.$get(key);
if (list) {
list = JSON.parse(list);
}
if (Array.isArray(list)) {
return list;
}
yield exports.set(key, JSON.stringify([]));
return [];
},
srem : function* srem(key, value) {
var count = 0;
var i;
var list = yield store.$get(key);
if (list) {
list = JSON.parse(list);
}
if (Array.isArray(list)) {
while ((i = list.indexOf(value)) > -1) {
list.splice(i, 1);
++count;
}
if (count > 0) {
yield exports.set(key, JSON.stringify(list));
}
}
else {
yield exports.set(key, JSON.stringify([]));
}
return count;
},
sadd : function* sadd(key, value) {
var list = yield store.$get(key);
value = Array.prototype.slice.call(1);
if (list) {
list = JSON.parse(list);
}
if (!Array.isArray(list)) {
list = [];
}
list.push.apply(list, value);
yield exports.set(key, JSON.stringify(list));
return list.length;
},
set : function* set(key, value) {
return yield store.$set(key, value, ONE_WEEK);
}
};