memoize-cache
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -16,2 +16,4 @@ var Promise = require('bluebird'); | ||
this._getMaxAge = opts.maxAge; | ||
this.serialize = opts.serialize || function (v) { return v; }; | ||
this.deserialize = opts.deserialize || function (v) { return v; }; | ||
@@ -36,3 +38,3 @@ this._maxValidity = typeof opts.maxValidity === 'undefined' ? | ||
var data = { data: output, maxValidity: maxValidity }; | ||
var data = { data: this.serialize(output), maxValidity: maxValidity }; | ||
var task = this.cacheManager.setAsync(k, data, maxAge ? {ttl: maxAge} : undefined); | ||
@@ -47,3 +49,3 @@ this._tasksToComplete.push(task); | ||
if (key === null) { | ||
// if k is null I don't cache | ||
// if k is null I don't cache | ||
return next(null, { | ||
@@ -67,3 +69,3 @@ cached: false, | ||
key: key, | ||
hit: res.data, | ||
hit: that.deserialize(res.data), | ||
stale: Boolean(res.maxValidity && res.maxValidity < Date.now()) | ||
@@ -70,0 +72,0 @@ }); |
{ | ||
"name": "memoize-cache", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "A cache support for memoized functions", | ||
@@ -28,4 +28,6 @@ "main": "index.js", | ||
"husky": "^0.10.2", | ||
"lzma-purejs": "^0.9.3", | ||
"mocha": "^2.1.0", | ||
"npm-release": "^1.0.0" | ||
"npm-release": "^1.0.0", | ||
"snappy": "^5.0.3" | ||
}, | ||
@@ -32,0 +34,0 @@ "dependencies": { |
@@ -38,2 +38,5 @@ var sizeof = require('sizeof'); | ||
this.serialize = opts.serialize || function (v) { return v; }; | ||
this.deserialize = opts.deserialize || function (v) { return v; }; | ||
this._maxLen = opts.maxLen || Infinity; | ||
@@ -53,3 +56,3 @@ } | ||
if(this._LRU.size() === this._maxLen) { | ||
if (this._LRU.size() === this._maxLen) { | ||
// remove from LRU heap | ||
@@ -63,3 +66,3 @@ lru = this._LRU.pop(); | ||
// add to cache | ||
this._cache[k] = { data: output, maxValidity: maxValidity }; | ||
this._cache[k] = { data: this.serialize(output), maxValidity: maxValidity }; | ||
@@ -113,3 +116,3 @@ if (this._maxLen !== Infinity) { | ||
if (key === null) { | ||
// if k is null I don't cache | ||
// if k is null I don't cache | ||
return next(null, { | ||
@@ -119,3 +122,3 @@ cached: false, | ||
}); | ||
} | ||
} | ||
@@ -140,3 +143,3 @@ if (key in this._cache) { | ||
key: key, | ||
hit: hit && hit.data, | ||
hit: hit && this.deserialize(hit.data), | ||
stale: hit && Boolean(hit.maxValidity && hit.maxValidity < Date.now()) | ||
@@ -143,0 +146,0 @@ }); |
@@ -18,2 +18,4 @@ memoize-cache | ||
* maxValidity: the maximum age of the item stored in the cache (in seconds) to be considered "not stale". Default: Infinity. You can also pass a function that will calculate the validity of a specific item. The function will take the same arguments as the "push" method (an array of inputs and the output). | ||
* serialize: it is an optional function that serialize the value stored (takes a value, returns a value). It can be used for pruning part of the object we don't want to save or even using a compression algorithm | ||
* deserialize: it is an optional function that deserialize the value stored (takes a value, returns a value). | ||
@@ -42,2 +44,4 @@ Example: | ||
* maxValidity: the maximum age of an item stored in the cache before being considered "stale" (in seconds). Default: Infinity. You can also pass a function that will calculate the validity of a specific item. The function will take the same arguments as the "push" method (an array of inputs and the output). | ||
* serialize: it is an optional function that serialize the value stored (takes a value, returns a value). It can be used for pruning part of the object we don't want to save or even using a compression algorithm | ||
* deserialize: it is an optional function that deserialize the value stored (takes a value, returns a value). | ||
@@ -44,0 +48,0 @@ Example: |
var assert = require('chai').assert; | ||
var Cache = require('../cache'); | ||
var cacheManager = require('cache-manager'); | ||
var lzma = require('lzma-purejs'); | ||
var snappy = require('snappy'); | ||
@@ -30,3 +32,3 @@ describe('cache-manager', function () { | ||
beforeEach(function () { | ||
memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); | ||
memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); | ||
}); | ||
@@ -44,3 +46,3 @@ | ||
}); | ||
}); | ||
}); | ||
@@ -59,3 +61,3 @@ it('must use value (2)', function (done) { | ||
}, 15); | ||
}); | ||
}); | ||
@@ -368,2 +370,52 @@ it('must use func', function (done) { | ||
}); | ||
it('must serialize/deserialize data with lzma', function (done) { | ||
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); | ||
var serialize = function (obj) { | ||
var data = new Buffer(JSON.stringify(obj), 'utf8'); | ||
var compressed = lzma.compressFile(data); | ||
return compressed; | ||
}; | ||
var deserialize = function (buf) { | ||
var uncompressed = lzma.decompressFile(buf); | ||
var data2 = new Buffer(uncompressed).toString('utf8'); | ||
return JSON.parse(data2); | ||
}; | ||
var cache = new Cache(memoryCache, {serialize: serialize, deserialize: deserialize}); | ||
cache.push([], 'result'); | ||
cache.query({}, function (err, res) { | ||
assert.equal(res.cached, true); | ||
assert.equal(res.key, '_default'); | ||
assert.equal(res.hit, 'result'); | ||
done(); | ||
}); | ||
}); | ||
it('must serialize/deserialize data with snappy', function (done) { | ||
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); | ||
var serialize = function (obj) { | ||
var data = new Buffer(JSON.stringify(obj), 'utf8'); | ||
var compressed = snappy.compressSync(data); | ||
return compressed; | ||
}; | ||
var deserialize = function (buf) { | ||
var uncompressed = snappy.uncompressSync(buf); | ||
var data2 = new Buffer(uncompressed).toString('utf8'); | ||
return JSON.parse(data2); | ||
}; | ||
var cache = new Cache(memoryCache, {serialize: serialize, deserialize: deserialize}); | ||
cache.push([], 'result'); | ||
cache.query({}, function (err, res) { | ||
assert.equal(res.cached, true); | ||
assert.equal(res.key, '_default'); | ||
assert.equal(res.hit, 'result'); | ||
done(); | ||
}); | ||
}); | ||
}); |
var assert = require('chai').assert; | ||
var Cache = require('../ram-cache'); | ||
var lzma = require('lzma-purejs'); | ||
var snappy = require('snappy'); | ||
@@ -55,3 +57,3 @@ describe('cache', function () { | ||
}, 15); | ||
}); | ||
}); | ||
@@ -378,2 +380,49 @@ it('must use func', function (done) { | ||
}); | ||
it('must serialize/deserialize data with lzma', function (done) { | ||
var serialize = function (obj) { | ||
var data = new Buffer(JSON.stringify(obj), 'utf8'); | ||
var compressed = lzma.compressFile(data); | ||
return compressed; | ||
}; | ||
var deserialize = function (buf) { | ||
var uncompressed = lzma.decompressFile(buf); | ||
var data2 = new Buffer(uncompressed).toString('utf8'); | ||
return JSON.parse(data2); | ||
}; | ||
var cache = new Cache({serialize: serialize, deserialize: deserialize}); | ||
cache.push([], 'result'); | ||
cache.query({}, function (err, res) { | ||
assert.equal(res.cached, true); | ||
assert.equal(res.key, '_default'); | ||
assert.equal(res.hit, 'result'); | ||
done(); | ||
}); | ||
}); | ||
it('must serialize/deserialize data with snappy', function (done) { | ||
var serialize = function (obj) { | ||
var data = new Buffer(JSON.stringify(obj), 'utf8'); | ||
var compressed = snappy.compressSync(data); | ||
return compressed; | ||
}; | ||
var deserialize = function (buf) { | ||
var uncompressed = snappy.uncompressSync(buf); | ||
var data2 = new Buffer(uncompressed).toString('utf8'); | ||
return JSON.parse(data2); | ||
}; | ||
var cache = new Cache({serialize: serialize, deserialize: deserialize}); | ||
cache.push([], 'result'); | ||
cache.query({}, function (err, res) { | ||
assert.equal(res.cached, true); | ||
assert.equal(res.key, '_default'); | ||
assert.equal(res.hit, 'result'); | ||
done(); | ||
}); | ||
}); | ||
}); |
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
49514
1251
123
8