-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.js
87 lines (82 loc) · 2.42 KB
/
cache.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
/**
* Asynchronous concurrent cache for node.js. Created for eliminate
* "dog-pile effect".
*/
var cache = {};
var waiters = {};
var needFlush = [];
/**
* Try to get value from cache for key.
*
* @example
*
* var cache = require("./cache");
*
* for (var i = 0; i < 100; i++) {
* cache("key",
* function(value){ // Getter
* console.log("Getted: " + value);
* },
* function(callback){
* setTimeout(function(){
* console.log("Do render");
* callback("test");
* }, 2000);
* }
* );
* }
*
* @param key Key in cache
* @param getter Function that fires if value for key is found.
* Takes one argument - value from cache.
* @param setter Function that fires if value for key is not found.
* Takes one argument - callback. Callback takes one argument too -
* generated value.
*/
function operate(key, getter, setter) {
if (key in cache) {
getter(cache[key]);
} else if (key in waiters) { // no cache look for waiters
waiters[key].push(getter);
} else { // no cache, no waiters - executing setter
waiters[key] = [];
waiters[key].push(getter);
setter(function(value) {
put(key, value);
});
}
}
exports.operate = operate;
/**
* Flush all cache keys with prefix. This operation will be executed
* after all waiters with this prefix is served.
* @param prefix Prefix of keys. If not defined - all cache will flushed.
*/
function flush(prefix) {
prefix = (prefix || "");
Object.keys(waiters).forEach(function(waiter) {
if (waiter.indexOf(prefix) == 0) {
needFlush.push(waiter);
}
});
// delete all not locked keys in cache
for (var key in cache) {
if (key.indexOf(prefix) == 0 && needFlush.indexOf(key) == -1) {
delete cache[key];
}
}
}
exports.flush = flush;
// private
function put(key, value) {
cache[key] = value;
waiters[key].forEach(function(action) {
action(value);
});
delete waiters[key];
var kill = needFlush.indexOf(key);
if (kill != -1) {
delete cache[key];
needFlush.splice(kill, 1);
}
}