memoize-cache
Advanced tools
Comparing version 0.0.2 to 0.1.0
103
index.js
@@ -1,102 +0,5 @@ | ||
var sizeof = require('sizeof'); | ||
var ramCache = require('./ram-cache'); | ||
/* | ||
Cache object | ||
*/ | ||
function Cache(opts) { | ||
opts = opts || {}; | ||
this._cache = {}; // key, value | ||
this._cacheKeys = []; // sorted by time {ts: xxx, key: xxx} new ones first | ||
this._getCacheKey = opts.key || function () { return '_default'; }; | ||
this._maxAge = opts.maxAge || Infinity; | ||
this._maxLen = opts.maxLen || Infinity; | ||
} | ||
Cache.prototype.push = function cache_push(args, output, next) { | ||
var k = this._getCacheKey.apply(undefined, args); | ||
if (typeof k !== 'string') { | ||
k = JSON.stringify(k); | ||
} | ||
if (k in this._cache) return next(undefined, k); | ||
this._cache[k] = output; | ||
this._cacheKeys.unshift({ | ||
key: k, | ||
ts: Date.now() | ||
}); | ||
this._purge(); | ||
next(undefined, k); | ||
module.exports = { | ||
ramCache: ramCache | ||
}; | ||
Cache.prototype._purge = function cache__purge() { | ||
// remove old entries | ||
var maxAge = this._maxAge; | ||
var maxLen = this._maxLen; | ||
var cache = this._cache; | ||
var now = Date.now(); | ||
this._cacheKeys = this._cacheKeys.filter(function (item) { | ||
if (item.ts + maxAge < now ) { | ||
delete cache[item.key]; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
// trim cache | ||
var keysToRemove = this._cacheKeys.slice(maxLen, Infinity); | ||
keysToRemove.forEach(function (item) { | ||
var k = item.key; | ||
delete cache[k]; | ||
}); | ||
this._cacheKeys = this._cacheKeys.slice(0, maxLen); | ||
}; | ||
Cache.prototype.reset = function cache_reset(next) { | ||
this._cache = {}; // key, value | ||
this._cacheKeys = []; // sorted by time {ts: xxx, key: xxx} | ||
next(); | ||
}; | ||
Cache.prototype.query = function cache_query(args, next) { | ||
var hit, | ||
cached = false, | ||
key; | ||
try { | ||
key = this._getCacheKey.apply(undefined, args); | ||
if (typeof key !== 'string') { | ||
key = JSON.stringify(key); | ||
} | ||
this._purge(); // purge stale cache entries | ||
if (key in this._cache) { | ||
cached = true; | ||
hit = this._cache[key]; // cache hit! | ||
} | ||
} | ||
catch (e) { | ||
return next(e); | ||
} | ||
next(undefined, { | ||
cached: cached, | ||
key: key, | ||
hit: hit | ||
}); | ||
}; | ||
Cache.prototype.size = function cache_size(pretty, next) { | ||
var size = sizeof.sizeof(this._cache, pretty); | ||
next(undefined, size); | ||
}; | ||
Cache.prototype.len = function cache_len(next) { | ||
var len = this._cacheKeys.length; | ||
next(undefined, len); | ||
}; | ||
module.exports = Cache; |
{ | ||
"name": "memoize-cache", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "A cache support for memoized functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ memoize-cache | ||
==================== | ||
This is an in-memory cache implementation. I have chosen a callback based asynchronous API. This is because ideally you can use the same API for implementing a in-file, network or DB based version of the same cache. | ||
This is an in-memory cache implementation. But the interface is designed to work with an external storage support (db, etc). The only 2 required public methods are: "push" and "query". | ||
@@ -33,5 +33,3 @@ Creating an instance | ||
```js | ||
cache.push(args, output, function (){ | ||
// cache updated | ||
}); | ||
cache.push(args, output); | ||
``` | ||
@@ -54,5 +52,3 @@ "args" is an array containing the arguments passed to the function that generated the output. | ||
```js | ||
cache.reset(function (err){ | ||
// the cache is empty now | ||
}); | ||
cache.reset(); | ||
``` | ||
@@ -63,5 +59,3 @@ | ||
```js | ||
cache.len(function (err, n){ | ||
// n is the number of item cached | ||
}); | ||
cache.len(); | ||
``` | ||
@@ -72,10 +66,6 @@ | ||
```js | ||
cache.size(true, function (err, size){ | ||
// size is an human readable size | ||
}); | ||
cache.size(true); // size is an human readable size | ||
cache.size(false, function (err, size){ | ||
// size is expressed in byte | ||
}); | ||
cache.size(false); // size is expressed in byte | ||
``` | ||
If the first argument is true the output will be pretty printed. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
12950
272
67
1