-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
121 lines (106 loc) · 2.68 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const _assign = require('lodash.assign');
let Memcached = require('memcached');
const MAX_EXPIRATION = 2592000; // memcached's max exp in seconds (30 days)
/**
* Cache
* @constructor
* @param {Object} config - cache key prefix
* @param {string} [config.keyPrefix] - cache key prefix
* @param {string} [config.cacheHost] - cache host url
* @param {Object} [options] - options passed to memcached
*
* @example
* new Cache('prefix', 'host');
*
*/
function Cache(config, options) {
this.config = _assign({
keyPrefix: '',
cacheHost: 'localhost:11211'
}, config);
options = options || {};
this._cache = new Memcached(this.config.cacheHost, options);
}
function getPromise(instance, method, key) {
return new Promise((resolve, reject) => {
const cb = (err, data) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
};
if (key) {
instance._cache[method](key, cb);
}
else {
instance._cache[method](cb);
}
});
}
function setPromise(instance, key, value, expires) {
return new Promise((resolve, reject) => {
instance._cache.set(key, value, expires, (err, data) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
/**
* get a cache item
* @param {string} key - cache key
* @returns {Promise}
*/
Cache.prototype.get = function(key) {
return getPromise(this, 'get', `${this.config.keyPrefix}${key}`);
};
/**
* get a cache item
* @param {string} key - full prefixed cache key
* @returns {Promise}
*/
Cache.prototype.utilGet = function(key) {
return getPromise(this, 'get', key);
};
/**
* get an object of cache items
* @param {array} keys - an array of cache keys
* @returns {Promise}
*/
Cache.prototype.getMulti = function(keys) {
return getPromise(this, 'getMulti', keys);
};
/**
* gets stats from memcached server
* @returns {Promise}
*/
Cache.prototype.stats = function() {
return getPromise(this, 'stats');
};
/**
* set an item in the cache
* @param {string} key - cache key
* @param {string|number|Object} data - data to set in cache
* @param {number} [expires=900] - expiration of data in seconds
* @returns {Promise}
*/
Cache.prototype.set = function(key, data, expires){
if ((new Date(expires * 1000).getTime() !== new Date(expires * 1000).getTime())){
throw new Error('Cache.set: Expiration must be seconds or timestamp');
}
return setPromise(this, this.config.keyPrefix + key, data, expires);
};
/**
* delete an item in the cache
* @param {string} key - cache key
* @returns {Promise}
*/
Cache.prototype.del = function(key) {
return getPromise(this, 'del', this.config.keyPrefix + key);
};
module.exports = Cache;