ttl-mem-cache
Advanced tools
@@ -6,3 +6,3 @@ 'use strict'; | ||
module.exports = class Cache extends stream.Duplex { | ||
constructor({ maxAge = 5 * 60 * 1000, stale = false } = {}) { | ||
constructor({ maxAge = 5 * 60 * 1000, stale = false, changefeed = false } = {}) { | ||
super({ | ||
@@ -13,2 +13,3 @@ objectMode: true | ||
this.stale = stale; | ||
this.changefeed = changefeed; | ||
this.store = new Map(); | ||
@@ -34,3 +35,3 @@ | ||
} | ||
return undefined; | ||
return null; | ||
} | ||
@@ -47,4 +48,17 @@ | ||
const expires = this.constructor._calculateExpire((maxAge || this.maxAge)); | ||
const eventObj = { | ||
key, | ||
value | ||
}; | ||
if (this.changefeed) { | ||
eventObj.value = { | ||
oldVal: this.get(key), | ||
newVal: value | ||
}; | ||
} | ||
this.store.set(key, { value, expires }); | ||
this.emit('set', { key, value }); | ||
this.emit('set', eventObj); | ||
return value; | ||
@@ -51,0 +65,0 @@ } |
{ | ||
"name": "ttl-mem-cache", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A in memory time to live cache with streaming support.", | ||
@@ -5,0 +5,0 @@ "main": "lib/cache.js", |
@@ -60,2 +60,3 @@ # ttl-mem-cache | ||
* stale - `Boolean` - If expired items in cache should be returned when pruned from the cache. Default: `false`. | ||
* changelog - `Boolean` - If emitted `set` event and stream should contain both old and new value. Default: `false` | ||
@@ -70,3 +71,3 @@ If an option Object with a `maxAge` is not provided all items in the cache will by | ||
for retrieving items from the cache. By default pruning happens before the method | ||
returns a value so if an item have expired, `undefined` will be returned for expired | ||
returns a value so if an item have expired, `null` will be returned for expired | ||
items. By setting `stale` to `true`, these methods will return the pruned item(s) | ||
@@ -117,4 +118,4 @@ before they are removed from the cache. | ||
the max age set on it, the item will be removed from the cache and this method | ||
will return `undefined` unless `stale` is set to `true` on the constructor. Then | ||
the expired item will be returned before its removed from the cache. | ||
will return `null` unless `stale` is set to `true` on the constructor. Then the | ||
expired item will be returned before its removed from the cache. | ||
@@ -213,2 +214,5 @@ | ||
If `changefeed` is set to be `true` on the constructor, the emitted Object will hold both | ||
old and new value for the key. See "changelog" for further info. | ||
### dispose | ||
@@ -316,4 +320,43 @@ | ||
If `changefeed` is set to be `true` on the constructor, the emitted Object in the Readable stream | ||
will hold both old and new value for the key. See "changelog" for further info. | ||
## Changelog | ||
If the attribute `changelog` is set to `true` on the constructor, some emitted events will | ||
emit an object holding both old and new values for the key. | ||
The emitted object looks like this: | ||
```js | ||
{ | ||
key: 'a', | ||
value: { | ||
oldVal: 'foo', | ||
newVal: 'bar' | ||
} | ||
} | ||
``` | ||
Example: | ||
```js | ||
const Cache = require('ttl-mem-cache'); | ||
const cache = new Cache({ changefeed: true }); | ||
cache.on('set', (item) => { | ||
// item will be in the format above | ||
}); | ||
cache.set('a', 'foo'); | ||
cache.set('a', 'bar'); | ||
``` | ||
If a key does not hold a value in cache before, `oldVal` will be `null`. | ||
If a key hold a value which has expired and `stale` is `false`, `oldVal` will be `null`. | ||
If a key hold a value which has expired and `stale` is `true`, `oldVal` will be the old value. | ||
## node.js compabillity | ||
@@ -320,0 +363,0 @@ |
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
44977
65.81%9
12.5%700
291.06%398
12.11%1
Infinity%