-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru.js
More file actions
80 lines (59 loc) · 1.34 KB
/
lru.js
File metadata and controls
80 lines (59 loc) · 1.34 KB
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
'use strict';
var LRUCache = require('lru-cache').LRUCache;
var lru = {
init: function(options) {
var cache, store;
var lruOptions = {};
if (options.maxSize) {
lruOptions.sizeCalculation = function(v) {
return JSON.stringify(v).length;
};
lruOptions.maxSize = options.maxSize;
} else {
lruOptions.max = options.max || 1000;
}
if (options.maxAge) {
lruOptions.ttl = options.maxAge;
}
if (options.dispose) {
lruOptions.dispose = options.dispose;
}
cache = new LRUCache(lruOptions);
store = {
lru: cache,
get: function(key, cb) {
var data = cache.get(key);
cb(null, data);
},
set: function(key, val, cb) {
cache.set(key, val);
if (cb) {
cb(null, val);
}
},
expire: function(key, cb) {
cache.delete(key);
if (cb) {
process.nextTick(function() { cb(null); });
}
},
reset: function() {
cache.clear();
},
size: function() {
return cache.size;
},
keycount: function() {
return this.size();
},
values: function() {
return Array.from(cache.values());
},
isReady: function() {
return true;
}
};
return store;
}
};
module.exports = lru;