exp-asynccache
Advanced tools
Comparing version
259
index.js
"use strict"; | ||
var Promise = require("bluebird"); | ||
var LRU = require("lru-cache-plus"); | ||
var EventEmitter = require("events"); | ||
var util = require("util"); | ||
function AsyncCache(cache) { | ||
this.cache = cache || new LRU(); | ||
this.pending = {}; | ||
const LRU = require("lru-cache-plus"); | ||
const EventEmitter = require("events"); | ||
if (typeof this.cache.on === "function") { | ||
var self = this; | ||
this.cache.on("error", function (err) { | ||
self.emit("error", err); | ||
}); | ||
class AsyncCache extends EventEmitter { | ||
constructor(cache) { | ||
super(); | ||
this.cache = cache || new LRU(); | ||
this.pending = {}; | ||
if (typeof this.cache.on === "function") { | ||
this.cache.on("error", (err) => { | ||
this.emit("error", err); | ||
}); | ||
} | ||
} | ||
EventEmitter.call(this); | ||
} | ||
util.inherits(AsyncCache, EventEmitter); | ||
AsyncCache.prototype.get = function (key, callback) { | ||
return Promise.resolve(this.cache.get(key)).then(function (data) { | ||
if (typeof callback === "function") { | ||
callback(null, data); | ||
async get(key, callback) { | ||
try { | ||
const data = await this.cache.get(key); | ||
if (typeof callback === "function") { | ||
return callback(null, data); | ||
} | ||
return data; | ||
} catch (err) { | ||
if (typeof callback === "function") { | ||
return callback(err); | ||
} | ||
} | ||
return data; | ||
}, function (err) { | ||
if (typeof callback === "function") { | ||
callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
} | ||
AsyncCache.prototype.has = function (key, callback) { | ||
return Promise.resolve(this.cache.has(key)).then(function (exists) { | ||
if (typeof callback === "function") { | ||
callback(null, exists); | ||
async has(key, callback) { | ||
try { | ||
const exists = await this.cache.has(key); | ||
if (typeof callback === "function") { | ||
return callback(null, exists); | ||
} | ||
return exists; | ||
} catch (err) { | ||
if (typeof callback === "function") { | ||
return callback(err); | ||
} | ||
} | ||
return exists; | ||
}, function (err) { | ||
if (typeof callback === "function") { | ||
callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
} | ||
AsyncCache.prototype.set = function (key, value, maxAge, callback) { | ||
if (typeof maxAge === "function") { | ||
callback = maxAge; | ||
maxAge = null; | ||
} | ||
return Promise.resolve(this.cache.set(key, value, maxAge)).then(function () { | ||
if (typeof callback === "function") { | ||
callback(); | ||
async set(key, value, maxAge, callback) { | ||
if (typeof maxAge === "function") { | ||
callback = maxAge; | ||
maxAge = null; | ||
} | ||
}, function (err) { | ||
if (typeof callback === "function") { | ||
callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
AsyncCache.prototype.del = function (key, callback) { | ||
return Promise.resolve(this.cache.del(key)).then(function () { | ||
if (typeof callback === "function") { | ||
callback(); | ||
try { | ||
await this.cache.set(key, value, maxAge); | ||
if (typeof callback === "function") { | ||
return callback(); | ||
} | ||
} catch (err) { | ||
if (typeof callback === "function") { | ||
return callback(err); | ||
} | ||
} | ||
}, function (err) { | ||
if (typeof callback === "function") { | ||
callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
} | ||
AsyncCache.prototype.reset = function (callback) { | ||
return Promise.resolve(this.cache.reset()).then(function () { | ||
if (typeof callback === "function") { | ||
callback(); | ||
async del(key, callback) { | ||
try { | ||
await this.cache.del(key); | ||
if (typeof callback === "function") { | ||
return callback(); | ||
} | ||
} catch (err) { | ||
if (typeof callback === "function") { | ||
return callback(err); | ||
} | ||
} | ||
}, function (err) { | ||
if (typeof callback === "function") { | ||
callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}; | ||
AsyncCache.prototype.lookup = function (key, resolveFn, hitFn) { | ||
var self = this; | ||
function get(key) { | ||
return Promise.resolve(self.cache.get(key)); | ||
} | ||
function set(key, value, maxAge) { | ||
return Promise.resolve(self.cache.set(key, value, maxAge)); | ||
async reset(callback) { | ||
try { | ||
await this.cache.reset(); | ||
if (typeof callback === "function") { | ||
return callback(); | ||
} | ||
} catch (err) { | ||
if (typeof callback === "function") { | ||
return callback(err); | ||
} | ||
} | ||
} | ||
function inner(hitFn) { | ||
function resolvedCallback(err, hit) { | ||
if (err) { | ||
if (self.pending[key]) { | ||
self.pending[key].forEach(function (callback) { | ||
setImmediate(callback, err); | ||
lookup(key, resolveFn, hitFn) { | ||
const resolvedCallback = async (...args) => { | ||
const [error, hit, ...rest] = args; | ||
if (error) { | ||
if (this.pending[key]) { | ||
this.pending[key].forEach((callback) => { | ||
setImmediate(callback, error); | ||
}); | ||
delete self.pending[key]; | ||
delete this.pending[key]; | ||
} | ||
@@ -122,62 +106,53 @@ return; | ||
// See https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments | ||
var args = new Array(arguments.length); | ||
args[0] = key; | ||
for (var i = 1; i < args.length; ++i) { | ||
args[i] = arguments[i]; | ||
let value; | ||
try { | ||
value = await this.cache.set(key, hit, ...rest); | ||
} catch (err) { | ||
this.emit("error", err); | ||
} | ||
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]; | ||
} | ||
}); | ||
} | ||
if (this.pending[key]) { | ||
this.pending[key].forEach((callback) => { | ||
setImmediate(callback, null, hit); | ||
}); | ||
delete this.pending[key]; | ||
} | ||
return get(key).catch(function (err) { | ||
self.emit("error", err); | ||
return undefined; | ||
}).then(function (value) { | ||
if (value === undefined) { | ||
return [false, value]; | ||
return value; | ||
}; | ||
const inner = async (innerHitFn) => { | ||
let value; | ||
try { | ||
value = await this.cache.get(key); | ||
} catch (err) { | ||
this.emit("error", err); | ||
} | ||
return [true, value]; | ||
}).catch(function (err) { | ||
self.emit("error", err); | ||
return [false, null]; | ||
}).then(function (arr) { | ||
var exists = arr[0]; | ||
var value = arr[1]; | ||
const exists = value !== undefined; | ||
if (exists) { | ||
return setImmediate(hitFn, null, value); | ||
return setImmediate(innerHitFn, null, value); | ||
} | ||
if (self.pending[key]) { | ||
self.pending[key].push(hitFn); | ||
if (this.pending[key]) { | ||
this.pending[key].push(innerHitFn); | ||
} else { | ||
self.pending[key] = [hitFn]; | ||
this.pending[key] = [innerHitFn]; | ||
resolveFn(resolvedCallback); | ||
} | ||
}); | ||
} | ||
}; | ||
if (hitFn) { | ||
inner(hitFn); | ||
} else { | ||
return new Promise(function (resolve, reject) { | ||
inner(function (err, hit) { | ||
if (err) return reject(err); | ||
return resolve(hit); | ||
if (hitFn) { | ||
inner(hitFn); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
inner((err, hit) => { | ||
if (err) return reject(err); | ||
return resolve(hit); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
module.exports = AsyncCache; |
{ | ||
"name": "exp-asynccache", | ||
"description": " A small async cache", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"author": "AB Kvällstidningen Expressen", | ||
@@ -13,3 +13,4 @@ "contributors": [ | ||
"scripts": { | ||
"test": "mocha --reporter spec" | ||
"test": "mocha --reporter spec", | ||
"posttest": "eslint --cache ." | ||
}, | ||
@@ -22,13 +23,10 @@ "main": "index.js", | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "http://github.com/ExpressenAB/exp-asynccache/raw/master/LICENSE" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"bluebird": "^2.3.10", | ||
"lru-cache-plus": "^2.5.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.9.2", | ||
"mocha": "^2.0.1" | ||
"chai": "^4.3.4", | ||
"eslint": "^8.1.0", | ||
"mocha": "^9.1.3" | ||
}, | ||
@@ -35,0 +33,0 @@ "files": [ |
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
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
1
-50%7836
-11.22%3
50%137
-14.91%- Removed
- Removed