exp-asynccache
Advanced tools
Comparing version
77
index.js
"use strict"; | ||
var Promise = require("bluebird"); | ||
var LRU = require("lru-cache-plus"); | ||
var EventEmitter = require("events"); | ||
var util = require("util"); | ||
@@ -8,10 +10,32 @@ function AsyncCache(cache) { | ||
this.pending = {}; | ||
if (typeof this.cache.on === "function") { | ||
var self = this; | ||
this.cache.on("error", function (err) { | ||
self.emit("error", err); | ||
}); | ||
} | ||
EventEmitter.call(this); | ||
} | ||
util.inherits(AsyncCache, EventEmitter); | ||
AsyncCache.prototype.lookup = function (key, resolveFn, hitFn) { | ||
var self = this; | ||
function get(key) { | ||
return Promise.resolve(self.cache.get(key)); | ||
} | ||
function set() { | ||
return Promise.resolve(self.cache.set.apply(self.cache, arguments)); | ||
} | ||
function has() { | ||
return Promise.resolve(self.cache.has(key)); | ||
} | ||
function inner(hitFn) { | ||
function resolvedCallback(err, hit, cacheHeader) { | ||
function resolvedCallback(err, hit) { | ||
if (err) { | ||
@@ -34,21 +58,42 @@ if (self.pending[key]) { | ||
self.cache.set.apply(self.cache, args); | ||
if (self.pending[key]) { | ||
self.pending[key].forEach(function (callback) { | ||
setImmediate(callback, null, hit); | ||
return set.apply(self.cache, args).catch(function (err) { | ||
self.emit("error", err); | ||
}).then(function () { | ||
if (self.pending[key]) { | ||
self.pending[key].forEach(function (callback) { | ||
setImmediate(callback, null, hit); | ||
}); | ||
delete self.pending[key]; | ||
} | ||
}); | ||
} | ||
return get(key).catch(function (err) { | ||
self.emit("error", err); | ||
return null; | ||
}).then(function (value) { | ||
if (value === null || value === undefined) { | ||
return has().then(function (exists) { | ||
return [exists, value]; | ||
}); | ||
delete self.pending[key]; | ||
} | ||
} | ||
return [true, value]; | ||
}).catch(function (err) { | ||
self.emit("error", err); | ||
return [false, null]; | ||
}).then(function (arr) { | ||
var exists = arr[0]; | ||
var value = arr[1]; | ||
if (self.cache.has(key)) { | ||
return setImmediate(hitFn, null, self.cache.get(key)); | ||
} | ||
if (exists) { | ||
return setImmediate(hitFn, null, value); | ||
} | ||
if (self.pending[key]) { | ||
self.pending[key].push(hitFn); | ||
} else { | ||
self.pending[key] = [hitFn]; | ||
resolveFn(resolvedCallback); | ||
} | ||
if (self.pending[key]) { | ||
self.pending[key].push(hitFn); | ||
} else { | ||
self.pending[key] = [hitFn]; | ||
resolveFn(resolvedCallback); | ||
} | ||
}); | ||
} | ||
@@ -55,0 +100,0 @@ |
{ | ||
"name": "exp-asynccache", | ||
"description": " A small async cache", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "AB Kvällstidningen Expressen", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
asynccache | ||
========== | ||
A async cache with a lookup function per key for node js with a different interface than async-cache. | ||
An async cache with a lookup function per key for node js with a different interface than async-cache. | ||
@@ -6,0 +6,0 @@ Errors are not cached and the callback function is always called asynchronously even if the value is resolved |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6597
19.97%95
69.64%1
Infinity%