memoize-cache
Advanced tools
Comparing version 1.0.1 to 1.0.2
16
cache.js
@@ -1,3 +0,3 @@ | ||
var md5omatic = require('md5-o-matic'); | ||
var Promise = require('bluebird'); | ||
var keyGetter = require('./key-getter'); | ||
@@ -13,16 +13,8 @@ /* | ||
Promise.promisifyAll(this.cacheManager); | ||
this._getCacheKey = key || function () { return '_default'; }; | ||
this.getCacheKey = keyGetter(key); | ||
this._tasksToComplete = []; | ||
} | ||
Cache.prototype.getCacheKey = function cache_getCacheKey(args) { | ||
var k = this._getCacheKey.apply(undefined, args); | ||
if (typeof k !== 'string') { | ||
k = md5omatic(JSON.stringify(k)); | ||
} | ||
return k; | ||
}; | ||
Cache.prototype.push = function cache_push(args, output) { | ||
var k = this.getCacheKey(args); | ||
var k = this.getCacheKey.apply(this, args); | ||
var task = this.cacheManager.setAsync(k, output); | ||
@@ -33,3 +25,3 @@ this._tasksToComplete.push(task); | ||
Cache.prototype.query = function cache_query(args, next) { | ||
var key = this.getCacheKey(args); | ||
var key = this.getCacheKey.apply(this, args); | ||
var that = this; | ||
@@ -36,0 +28,0 @@ |
var ramCache = require('./ram-cache'); | ||
var cache = require('./cache'); | ||
var keyGetter = require('./key-getter'); | ||
module.exports = { | ||
ramCache: ramCache, | ||
cache: cache | ||
cache: cache, | ||
keyGetter: keyGetter, | ||
}; |
{ | ||
"name": "memoize-cache", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A cache support for memoized functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var sizeof = require('sizeof'); | ||
var Heap = require('./heap'); | ||
var md5omatic = require('md5-o-matic'); | ||
var keyGetter = require('./key-getter'); | ||
/* | ||
@@ -34,3 +35,3 @@ | ||
this._getCacheKey = opts.key || function () { return '_default'; }; | ||
this.getCacheKey = keyGetter(opts.key); | ||
this._maxAge = opts.maxAge || Infinity; | ||
@@ -40,13 +41,5 @@ this._maxLen = opts.maxLen || Infinity; | ||
Cache.prototype.getCacheKey = function cache_getCacheKey(args) { | ||
var k = this._getCacheKey.apply(undefined, args); | ||
if (typeof k !== 'string') { | ||
k = md5omatic(JSON.stringify(k)); | ||
} | ||
return k; | ||
}; | ||
Cache.prototype.push = function cache_push(args, output) { | ||
var lru, oldestIndex, | ||
k = this.getCacheKey(args); | ||
k = this.getCacheKey.apply(this, args); | ||
@@ -118,3 +111,3 @@ if (k in this._cache) return; | ||
key = this.getCacheKey(args); | ||
key = this.getCacheKey.apply(this, args); | ||
@@ -121,0 +114,0 @@ if (key in this._cache) { |
@@ -74,2 +74,10 @@ memoize-cache | ||
Getting the cache key | ||
--------------------- | ||
```js | ||
var key = cache.getCacheKey(...); | ||
``` | ||
It takes as arguments the same arguments of the function. It returns the cache key. | ||
It uses the function passed in the factory function. If it returns a string it uses it as key. In case it is not a string it tries to serialize it to JSON and then to an hash (using md5). | ||
resetting the cache | ||
@@ -98,1 +106,10 @@ ------------------- | ||
If the first argument is true the output will be pretty printed. | ||
key-getter | ||
========== | ||
This is a utility function used to generate a function that returns a key. | ||
```js | ||
var keyGetter = require('memoize-cache/key-getter') | ||
var getKey = keyGetter(func); | ||
``` | ||
It takes a function as argument (returning a key). But it takes care of checking if it is a valid key and to hash it. |
@@ -10,5 +10,5 @@ var assert = require('chai').assert; | ||
var cache = new Cache(memoryCache, function (n) {return n;}); | ||
assert.equal(cache.getCacheKey(['1']), '1'); | ||
assert.equal(cache.getCacheKey([1]), 'c4ca4238a0b923820dcc509a6f75849b'); | ||
assert.equal(cache.getCacheKey([{d:1}]), 'dc6f789c90af7a7f8156af120f33e3be'); | ||
assert.equal(cache.getCacheKey('1'), '1'); | ||
assert.equal(cache.getCacheKey(1), 'c4ca4238a0b923820dcc509a6f75849b'); | ||
assert.equal(cache.getCacheKey({d:1}), 'dc6f789c90af7a7f8156af120f33e3be'); | ||
}); | ||
@@ -15,0 +15,0 @@ |
@@ -8,5 +8,5 @@ var assert = require('chai').assert; | ||
var cache = new Cache({key: function (n) {return n;}}); | ||
assert.equal(cache.getCacheKey(['1']), '1'); | ||
assert.equal(cache.getCacheKey([1]), 'c4ca4238a0b923820dcc509a6f75849b'); | ||
assert.equal(cache.getCacheKey([{d:1}]), 'dc6f789c90af7a7f8156af120f33e3be'); | ||
assert.equal(cache.getCacheKey('1'), '1'); | ||
assert.equal(cache.getCacheKey(1), 'c4ca4238a0b923820dcc509a6f75849b'); | ||
assert.equal(cache.getCacheKey({d:1}), 'dc6f789c90af7a7f8156af120f33e3be'); | ||
}); | ||
@@ -13,0 +13,0 @@ |
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
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
33747
15
859
114