ttl-mem-cache
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
'use strict' | ||
'use strict'; | ||
@@ -8,5 +8,5 @@ const benchmark = require('benchmark'); | ||
function add(name, fn) { | ||
const add = (name, fn) => { | ||
suite.add(name, fn); | ||
} | ||
}; | ||
@@ -23,7 +23,7 @@ | ||
add('.set()', () => { | ||
cache1.set('key' + (cache1Counter++), 'value'); | ||
cache1.set(`key${cache1Counter++}`, 'value'); | ||
}); | ||
add('.set(maxAge)', () => { | ||
cache1.set('key' + (cache1Counter++), 'value', 3600000); | ||
cache1.set(`key${cache1Counter++}`, 'value', 3600000); | ||
}); | ||
@@ -41,7 +41,7 @@ | ||
for (let i = 0; i < 10000; i++) { | ||
cache2.set('key' + i, 'value'); | ||
cache2.set(`key${i}`, 'value'); | ||
} | ||
add('.get()', () => { | ||
cache2.get('key' + (cache2Counter++) % 10000); | ||
cache2.get(`key${(cache2Counter++) % 10000}`); | ||
}); | ||
@@ -56,6 +56,5 @@ | ||
const cache3 = new Cache(); | ||
let cache3Counter = 0; | ||
for (let i = 0; i < 10000; i++) { | ||
cache3.set('key' + i, 'value'); | ||
cache3.set(`key${i}`, 'value'); | ||
} | ||
@@ -69,6 +68,5 @@ | ||
const cache4 = new Cache(); | ||
let cache4Counter = 0; | ||
for (let i = 0; i < 10000; i++) { | ||
cache4.set('key' + i, 'value'); | ||
cache4.set(`key${i}`, 'value'); | ||
} | ||
@@ -85,7 +83,8 @@ | ||
suite | ||
.on('cycle', (event) => { | ||
console.log(String(event.target)) | ||
if (event.target.error) | ||
console.error(event.target.error) | ||
}) | ||
.run() | ||
.on('cycle', (event) => { | ||
console.log(event.target.toString()); | ||
if (event.target.error) { | ||
console.error(event.target.error); | ||
} | ||
}) | ||
.run(); |
@@ -5,4 +5,2 @@ 'use strict'; | ||
module.exports = class Cache extends stream.Duplex { | ||
@@ -40,3 +38,3 @@ constructor({ maxAge = 5 * 60 * 1000 } = {}) { | ||
const expires = Date.now() + (maxAge ? maxAge : this.maxAge); | ||
const expires = Date.now() + (maxAge || this.maxAge); | ||
this.store.set(key, { value, expires }); | ||
@@ -48,3 +46,3 @@ this.emit('set', { key, value }); | ||
del(key) { | ||
const item = this.store.delete(key); | ||
this.store.delete(key); | ||
this.emit('dispose', key); | ||
@@ -63,3 +61,3 @@ return undefined; | ||
arr.push(mutator({ | ||
key: key, | ||
key, | ||
value: item.value | ||
@@ -69,3 +67,3 @@ })); | ||
arr.push({ | ||
key: key, | ||
key, | ||
value: item.value | ||
@@ -82,2 +80,11 @@ }); | ||
prune() { | ||
const now = Date.now(); | ||
this.store.forEach((item, key) => { | ||
if (!this.constructor._validate(item, now)) { | ||
this.del(key); | ||
} | ||
}); | ||
} | ||
_write(obj, enc, next) { | ||
@@ -95,13 +102,12 @@ if (obj.key && obj.value) { | ||
this.emit('error', new Error('Object does not contain a "key" property or the value for "key" is null or undefined')); | ||
next(); | ||
return next(); | ||
} | ||
_read(size) { | ||
_read() { | ||
// "push" happens on "set" event in constructor | ||
} | ||
static _validate(item = {expires: 0}, now = Date.now()) { | ||
static _validate(item = { expires: 0 }, now = Date.now()) { | ||
return (item.expires > now); | ||
} | ||
}; |
{ | ||
"name": "ttl-mem-cache", | ||
"version": "1.0.0", | ||
"description": "", | ||
"version": "1.0.1", | ||
"description": "A in memory time to live cache with streaming support.", | ||
"main": "lib/cache.js", | ||
@@ -10,4 +10,5 @@ "directories": { | ||
"scripts": { | ||
"bench": "node benchmark/cache.js", | ||
"test": "tap test/*.js", | ||
"bench": "node benchmark/cache.js" | ||
"lint": "eslint ." | ||
}, | ||
@@ -19,3 +20,6 @@ "repository": { | ||
"keywords": [ | ||
"cache" | ||
"cache", | ||
"ttl", | ||
"time to live", | ||
"performance" | ||
], | ||
@@ -29,2 +33,5 @@ "author": "Trygve Lie", | ||
"devDependencies": { | ||
"eslint": "4.7.0", | ||
"eslint-config-airbnb-base": "12.0.0", | ||
"eslint-plugin-import": "2.7.0", | ||
"benchmark": "2.1.4", | ||
@@ -31,0 +38,0 @@ "lolex": "2.1.2", |
@@ -158,3 +158,8 @@ # ttl-mem-cache | ||
### .prune() | ||
Iterates over all items in the cache and proactively prunes expired items. | ||
## Events | ||
@@ -161,0 +166,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
25065
8
146
327
6