-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
209 lines (171 loc) · 2.91 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* dependencies
*/
var ms = require('ms');
/**
* Export `Cache`
*/
module.exports = Cache;
/**
* Has.
*/
var has = ({}).hasOwnProperty;
/**
* Initialize `Cache`.
*
* @param {Object} opts
* @api public
*/
function Cache(opts){
if (!(this instanceof Cache)) return new Cache(opts);
opts = opts || {};
opts.max = opts.max || Infinity;
opts.ttl = opts.ttl || 0;
this.keys = [];
this.vals = {};
this.max(opts.max);
this.ttl(opts.ttl);
}
/**
* Set `ttl`.
*
* @param {Number|String} ttl
* @return {Cache}
* @api public
*/
Cache.prototype.ttl = function(ttl){
this._ttl = 'string' == typeof ttl
? ms(ttl)
: ttl;
return this;
};
/**
* Set `key`, `val` and optional `ttl`
*
* @param {String|Object} key
* @param {Mixed} val
* @param {String|Number} ttl
* @return {Cache}
* @api public
*/
Cache.prototype.set = function(key, val, ttl){
if ('object' == typeof key) {
for (var k in key) this.set(k, key[k], ttl);
return this;
}
// remove
if (this.has(key)) {
this.remove(key);
}
// ttl
if ('string' == typeof ttl) ttl = ms(ttl);
// add
this.keys.push(key);
this.vals[key] = {
ttl: ttl || this._ttl,
created: +new Date,
value: val
};
// cap
this.cap();
return this;
};
/**
* Get `key`.
*
* @param {String} key
* @return {Mixed}
* @api public
*/
Cache.prototype.get = function(key){
if (!this.has(key)) return;
var val = this.vals[key];
// ttl
if (val.ttl && new Date > val.ttl + val.created) {
this.remove(key);
return;
}
// promote
this.promote(key);
// value
return val.value;
};
/**
* Has `key`
*
* @param {String} key
* @return {Boolean}
* @api public
*/
Cache.prototype.has = function(key){
return has.call(this.vals, key);
};
/**
* Remove `key`
*
* @param {String} key
* @return {Cache}
* @api public
*/
Cache.prototype.remove = function(key){
if (!this.has(key)) return this;
var i = this.keys.indexOf(key);
this.keys.splice(i, 1);
delete this.vals[key];
return this;
};
/**
* Set `max`.
*
* @param {Number} max
* @return {Cache}
* @api public
*/
Cache.prototype.max = function(max){
this._max = max;
this.cap();
return this;
};
/**
* To json
*
* @return {Object}
* @api public
*/
Cache.prototype.toJSON = function(){
var ret = {};
for (var k in this.vals) {
if (!has.call(this.vals, k)) continue;
ret[k] = this.vals[k].value;
}
return ret;
};
/**
* Promote `key`
*
* @param {String} key
* @return {Cache}
* @api public
*/
Cache.prototype.promote = function(key){
if (!this.has(key)) return this;
var i = this.keys.indexOf(key);
this.keys.splice(i, 1);
this.keys.push(key);
return this;
};
/**
* Cap
*
* @return {Cache}
* @api private
*/
Cache.prototype.cap = function(){
var length = this.keys.length
, n = length - this._max;
while (0 < n--) {
var k = this.keys.shift();
delete this.vals[k];
}
return this;
};