/usr/lib/nodejs/tilelive-mapnik/lockingcache.js is in node-tilelive-mapnik 0.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | module.exports = LockingCache;
function LockingCache(generate, timeout) {
this.callbacks = {};
this.timeouts = {};
this.results = {};
// When there's no generator function, you
this.generate = generate || function() {};
// Timeout cached objects after 1 minute by default.
// A value of 0 will cause it to not cache at all beyond, just return the result to
// all locked requests.
this.timeout = (typeof timeout === "undefined") ? 60000 : timeout;
}
LockingCache.prototype.get = function(id, callback) {
if (!this.callbacks[id]) this.callbacks[id] = [];
this.callbacks[id].push(callback);
if (this.results[id]) {
this.trigger(id);
} else {
var ids = this.generate.call(this, id);
if (!ids || ids.indexOf(id) < 0) {
this.put(id, new Error("Generator didn't generate this item"));
} else ids.forEach(function(id) {
this.results[id] = this.results[id] || true;
}, this);
}
};
LockingCache.prototype.del = function(id) {
delete this.results[id];
delete this.callbacks[id];
if (this.timeouts[id]) {
clearTimeout(this.timeouts[id]);
delete this.timeouts[id];
}
};
LockingCache.prototype.put = function(id) {
if (this.timeout > 0) {
this.timeouts[id] = setTimeout(this.del.bind(this, id), this.timeout);
}
this.results[id] = Array.prototype.slice.call(arguments, 1);
if (this.callbacks[id] && this.callbacks[id].length) {
this.trigger(id);
}
};
LockingCache.prototype.clear = function() {
for (var id in this.timeouts) {
this.del(id);
}
};
LockingCache.prototype.trigger = function(id) {
if (this.results[id] && this.results[id] !== true) {
process.nextTick(function() {
var data = this.results[id];
var callbacks = this.callbacks[id] || [];
if (this.timeout === 0) {
// instant purge with the first put() for a key
// clears timeouts and results
this.del(id);
} else {
delete this.callbacks[id];
}
callbacks.forEach(function(callback) {
callback.apply(callback, data);
});
}.bind(this));
}
};
|