/
cache.js
90 lines (84 loc) · 2.09 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
88
89
90
/**
* Asynchronous concurrent cache for node.js. Created for eliminate
* "dog-pile effect".
*/
//var sys = require("sys");
cache = {};
locks = {};
needFlush = [];
/**
* Try to get value from cache for key.
*
* @example
*
* var sys = require("sys");
* var cache = require("./cache");
*
* for (var i = 0; i < 100; i++) {
* cache("key",
* function(value){ // Getter
* sys.debug("Getted: " + value);
* },
* function(callback){
* setTimeout(function(){
* sys.debug("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 get(key, getter, setter) {
if (key in cache) {
getter(cache[key]);
} else if (key in locks) { // no cache look for locks
locks[key].push(getter);
} else { // no cache, no locks - executing setter
locks[key] = [];
locks[key].push(getter);
setter(function(value) {
put(key, value);
});
}
}
exports.get = get;
/**
* Flush all cache keys with prefix. This operation will be executed
* after all locks with this prefix is served.
* @param prefix Prefix of keys. If not defined - all cache will flushed.
*/
function flush(prefix) {
prefix = (prefix || "");
Object.keys(locks).forEach(function(lock) {
if (lock.indexOf(prefix) == 0) {
needFlush.push(lock);
}
});
// 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;
locks[key].forEach(function(action) {
action(value);
});
delete locks[key];
var kill = needFlush.indexOf(key);
if (kill != -1) {
delete cache[key];
needFlush.splice(kill, 1);
}
}