ttl-mem-cache
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -9,2 +9,3 @@ 'use strict'; | ||
// https://github.com/mcollina/split2 | ||
@@ -11,0 +12,0 @@ const toJson = new stream.Transform({ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const crypto = require('crypto'); | ||
const utils = require('./utils'); | ||
@@ -10,3 +11,3 @@ const _set = Symbol('_set'); | ||
module.exports = class TtlMemCache extends stream.Duplex { | ||
const TtlMemCache = class TtlMemCache extends stream.Duplex { | ||
constructor({ | ||
@@ -71,3 +72,3 @@ maxAge = 5 * 60 * 1000, stale = false, changefeed = false, id = undefined | ||
const expires = this.constructor._calculateExpire((options.maxAge || this.maxAge)); | ||
const expires = utils.calculateExpire((options.maxAge || this.maxAge)); | ||
@@ -112,3 +113,3 @@ const eventObj = { | ||
if (item) { | ||
if (this.constructor._validate(item)) { | ||
if (utils.validate(item)) { | ||
return item.value; | ||
@@ -142,3 +143,3 @@ } | ||
this.store.forEach((item, key) => { | ||
if (this.constructor._validate(item, now)) { | ||
if (utils.validate(item, now)) { | ||
if (mutate) { | ||
@@ -167,3 +168,3 @@ arr.push(mutator(item.value)); | ||
this.store.forEach((item, key) => { | ||
if (!this.constructor._validate(item, now)) { | ||
if (!utils.validate(item, now)) { | ||
this.del(key); | ||
@@ -230,20 +231,4 @@ } | ||
} | ||
}; | ||
/** | ||
* Static methods | ||
*/ | ||
static _calculateExpire(maxAge = 0) { | ||
if (maxAge === Infinity) { | ||
return maxAge; | ||
} | ||
return Date.now() + maxAge; | ||
} | ||
static _validate(item = { expires: 0 }, now = Date.now()) { | ||
if (item.expires === Infinity) { | ||
return false; | ||
} | ||
return (item.expires > now); | ||
} | ||
}; | ||
module.exports = TtlMemCache; |
{ | ||
"name": "ttl-mem-cache", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "A in memory time to live cache with streaming support.", | ||
@@ -12,2 +12,3 @@ "main": "lib/cache.js", | ||
"test": "tap test/*.js", | ||
"test:coverage": "tap test/*.js --cov", | ||
"lint": "eslint .", | ||
@@ -37,9 +38,9 @@ "lint:fix": "eslint . --fix" | ||
"devDependencies": { | ||
"eslint": "4.11.0", | ||
"eslint": "4.14.0", | ||
"eslint-config-airbnb-base": "12.1.0", | ||
"eslint-plugin-import": "2.8.0", | ||
"benchmark": "2.1.4", | ||
"lolex": "2.3.0", | ||
"tap": "10.7.3" | ||
"lolex": "2.3.1", | ||
"tap": "11.0.1" | ||
} | ||
} |
@@ -197,8 +197,13 @@ # ttl-mem-cache | ||
### .load() | ||
### .load(dump) | ||
Loads an Array of items, provided by `.dump()`, into the cache. If any of | ||
the items in the loaded Array contains a key which already are in the cache | ||
the entry in the cache will be overwritten. | ||
Loads an Array of items, provided by `.dump()`, into the cache. | ||
This method take the following arguments: | ||
* dump - Array of items to be imported. | ||
If any of the items in the loaded Array contains a key which already are in | ||
the cache the entry in the cache will be overwritten. | ||
If any of the entries in the loaded Array are not compatible with the format | ||
@@ -205,0 +210,0 @@ which `.dump()` exports, they will not be inserted into the cache. |
'use strict'; | ||
const stream = require('readable-stream'); | ||
const stream = require('stream'); | ||
const Cache = require('../'); | ||
@@ -410,2 +410,39 @@ const lolex = require('lolex'); | ||
const entries1 = cache.entries((value) => { | ||
return `prefix-${value}`; | ||
}); | ||
t.equal(entries1[0], 'prefix-bar'); | ||
t.equal(entries1[1], 'prefix-foo'); | ||
t.equal(entries1[2], 'prefix-xyz'); | ||
t.equal(entries1.length, 3); | ||
clock.tick(3000); | ||
const entries2 = cache.entries((value) => { | ||
return `prefix-${value}`; | ||
}); | ||
t.equal(entries2[0], 'prefix-bar'); | ||
t.equal(entries2[1], 'prefix-foo'); | ||
t.equal(entries2[2], 'prefix-xyz'); | ||
t.equal(entries2.length, 3); | ||
const entries3 = cache.entries((value) => { | ||
return `prefix-${value}`; | ||
}); | ||
t.equal(entries3[0], 'prefix-bar'); | ||
t.equal(entries3[1], 'prefix-xyz'); | ||
t.equal(entries3.length, 2); | ||
clock.uninstall(); | ||
t.end(); | ||
}); | ||
tap.test('cache.entries() - cache set to return stale items, call with mutator - purged items should be mutated and returned once', (t) => { | ||
const clock = lolex.install(); | ||
const cache = new Cache({ stale: true }); | ||
cache.set('a', 'bar'); | ||
cache.set('b', 'foo', 2 * 1000); | ||
cache.set('c', 'xyz'); | ||
const entries1 = cache.entries(); | ||
@@ -427,3 +464,2 @@ t.equal(entries1.length, 3); | ||
/** | ||
@@ -674,4 +710,25 @@ * .prune() | ||
tap.test('cache.dump().load() - dump entries from one cache - should import into secondary cache and overwrite existing entries', (t) => { | ||
const cacheA = new Cache(); | ||
const cacheB = new Cache(); | ||
cacheA.set('a', 'bar'); | ||
cacheA.set('b', 'foo'); | ||
cacheB.set('a', 'xyz'); | ||
cacheB.set('b', 'zyx'); | ||
t.equal(cacheB.get('a'), 'xyz'); | ||
t.equal(cacheB.get('b'), 'zyx'); | ||
cacheB.load(cacheA.dump()); | ||
t.equal(cacheB.get('a'), 'bar'); | ||
t.equal(cacheB.get('b'), 'foo'); | ||
t.end(); | ||
}); | ||
/** | ||
@@ -804,3 +861,2 @@ * .length() | ||
/** | ||
@@ -850,2 +906,9 @@ * ._read() - Stream | ||
tap.test('_read() - set item when no stream is attached to Readable stream - should be no items in internal stream buffer', (t) => { | ||
const cache = new Cache(); | ||
cache.set('a', 'foo'); | ||
cache.set('a', 'bar'); | ||
t.equal(cache._readableState.buffer.length, 0); | ||
t.end(); | ||
}); | ||
@@ -923,49 +986,1 @@ | ||
}); | ||
/** | ||
* ._calculateExpire() | ||
*/ | ||
tap.test('_calculateExpire() - "maxAge" is Infinity - should return Infinity', (t) => { | ||
t.equal(Cache._calculateExpire(Infinity), Infinity); | ||
t.end(); | ||
}); | ||
tap.test('_calculateExpire() - "maxAge" is a number - should return now timestamp plus the number', (t) => { | ||
const clock = lolex.install({ now: 2000 }); | ||
t.equal(Cache._calculateExpire(2000), 4000); | ||
clock.uninstall(); | ||
t.end(); | ||
}); | ||
/** | ||
* ._validate() | ||
*/ | ||
tap.test('_validate() - empty argument - should return false', (t) => { | ||
t.equal(Cache._validate(), false); | ||
t.end(); | ||
}); | ||
tap.test('_validate() - "expires" is Infinity - should return false', (t) => { | ||
const expires = Infinity; | ||
t.equal(Cache._validate({ expires }), false); | ||
t.end(); | ||
}); | ||
tap.test('_validate() - "expires" is behind Date.now() - should return false', (t) => { | ||
const expires = Date.now() - 100000; | ||
t.equal(Cache._validate({ expires }), false); | ||
t.end(); | ||
}); | ||
tap.test('_validate() - "expires" is in front of Date.now() - should return true', (t) => { | ||
const expires = Date.now() + 100000; | ||
t.equal(Cache._validate({ expires }), true); | ||
t.end(); | ||
}); |
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
78109
15
1241
462