Comparing version 2.0.0 to 3.0.0
@@ -8,3 +8,3 @@ /** | ||
* @link https://github.com/avoidwork/tiny-lru | ||
* @version 2.0.0 | ||
* @version 3.0.0 | ||
*/ | ||
@@ -15,7 +15,6 @@ "use strict"; | ||
const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1), | ||
empty = ""; | ||
empty = null; | ||
class LRU { | ||
constructor (max, notify, ttl, expire) { | ||
this.expire = expire; | ||
constructor (max, notify, ttl) { | ||
this.max = max; | ||
@@ -31,3 +30,3 @@ this.notify = notify; | ||
if (!silent && this.notify) { | ||
if (silent === false && this.notify === true) { | ||
next(this.onchange("clear", this.dump())); | ||
@@ -39,11 +38,2 @@ } | ||
clearTimer (key, col = "timers") { | ||
if (this.has(key, col)) { | ||
clearTimeout(this[col][key]); | ||
delete this[col][key]; | ||
} | ||
return this; | ||
} | ||
delete (key, silent = false) { | ||
@@ -54,7 +44,11 @@ return this.remove(key, silent); | ||
dump () { | ||
const obj = JSON.parse(JSON.stringify(this)); | ||
delete obj.timers; | ||
return JSON.stringify(obj); | ||
return JSON.stringify({ | ||
cache: this.cache, | ||
first: this.first, | ||
last: this.last, | ||
length: this.length, | ||
max: this.max, | ||
notify: this.notify, | ||
ttl: this.ttl | ||
}); | ||
} | ||
@@ -65,3 +59,3 @@ | ||
if (this.notify) { | ||
if (this.notify === true) { | ||
next(this.onchange("evict", this.dump())); | ||
@@ -76,12 +70,19 @@ } | ||
if (this.has(key)) { | ||
output = this.cache[key].value; | ||
this.set(key, output); | ||
if (this.has(key) === true) { | ||
const item = this.cache[key]; | ||
if (this.notify) { | ||
next(this.onchange("get", this.dump())); | ||
} | ||
if (item.expiry === -1 || item.expiry <= Date.now()) { | ||
output = item.value; | ||
if (this.ttl > 0) { | ||
this.clearTimer(key).setTimer(key); | ||
if (this.first !== empty) { | ||
this.cache[this.first].next = key; | ||
} | ||
this.first = key; | ||
if (this.notify === true) { | ||
next(this.onchange("get", this.dump())); | ||
} | ||
} else { | ||
this.remove(key); | ||
} | ||
@@ -93,4 +94,4 @@ } | ||
has (key, type = "cache") { | ||
return key in this[type]; | ||
has (key) { | ||
return key in this.cache; | ||
} | ||
@@ -103,4 +104,4 @@ | ||
if (this.has(key)) { | ||
const cached = this.cache[key]; | ||
if (this.has(key) === true) { | ||
result = this.cache[key]; | ||
@@ -110,15 +111,7 @@ delete this.cache[key]; | ||
if (this.ttl > 0) { | ||
this.clearTimer(key); | ||
} | ||
if (result.previous !== empty) { | ||
this.cache[result.previous].next = result.next; | ||
if (silent === false && this.expire > 0) { | ||
this.clearTimer(key, "expires"); | ||
} | ||
if (this.has(cached.previous)) { | ||
this.cache[cached.previous].next = cached.next; | ||
if (this.first === key) { | ||
this.first = cached.previous; | ||
this.first = result.previous; | ||
} | ||
@@ -129,7 +122,7 @@ } else if (this.first === key) { | ||
if (this.has(cached.next)) { | ||
this.cache[cached.next].previous = cached.previous; | ||
if (result.next !== empty) { | ||
this.cache[result.next].previous = result.previous; | ||
if (this.last === key) { | ||
this.last = cached.next; | ||
this.last = result.next; | ||
} | ||
@@ -140,9 +133,7 @@ } else if (this.last === key) { | ||
result = cached; | ||
if (silent === false && this.notify === true) { | ||
next(this.onchange("remove", this.dump())); | ||
} | ||
} | ||
if (!silent && this.notify) { | ||
next(this.onchange("remove", this.dump())); | ||
} | ||
return result; | ||
@@ -152,16 +143,6 @@ } | ||
reset () { | ||
if (this.expires !== void 0) { | ||
Object.keys(this.expires).forEach(i => this.clearTimer(i, "expires")); | ||
} | ||
if (this.timers !== void 0) { | ||
Object.keys(this.timers).forEach(i => this.clearTimer(i)); | ||
} | ||
this.cache = {}; | ||
this.expires = {}; | ||
this.first = empty; | ||
this.last = empty; | ||
this.length = 0; | ||
this.timers = {}; | ||
@@ -172,6 +153,5 @@ return this; | ||
set (key, value) { | ||
let first, item; | ||
if (this.has(key) === true) { | ||
const item = this.cache[key]; | ||
if (this.has(key)) { | ||
item = this.cache[key]; | ||
item.value = value; | ||
@@ -188,11 +168,9 @@ item.next = empty; | ||
} else { | ||
if (++this.length > this.max) { | ||
this.remove(this.last, true); | ||
if (this.length === this.max) { | ||
this.evict(); | ||
} | ||
if (this.length === 1) { | ||
this.last = key; | ||
} | ||
this.length++; | ||
this.cache[key] = { | ||
expiry: this.ttl > 0 ? new Date().getTime() + this.ttl : -1, | ||
next: empty, | ||
@@ -202,6 +180,11 @@ previous: this.first, | ||
}; | ||
if (this.length === 1) { | ||
this.last = key; | ||
} | ||
} | ||
if (this.first !== key && this.has(this.first)) { | ||
first = this.cache[this.first]; | ||
if (this.first !== empty && this.first !== key) { | ||
const first = this.cache[this.first]; | ||
first.next = key; | ||
@@ -216,25 +199,9 @@ | ||
if (this.notify) { | ||
if (this.notify === true) { | ||
next(this.onchange("set", this.dump())); | ||
} | ||
if (this.ttl > 0) { | ||
this.clearTimer(key).setTimer(key); | ||
} | ||
if (this.expire > 0 && this.has(key, "expires") === false) { | ||
this.setExpire(key); | ||
} | ||
return this; | ||
} | ||
setExpire (key) { | ||
this.expires[key] = setTimeout(() => this.clearTimer(key, "expires").clearTimer(key).remove(key), this.expire); | ||
} | ||
setTimer (key) { | ||
this.timers[key] = setTimeout(() => this.remove(key), this.ttl); | ||
} | ||
update (arg) { | ||
@@ -241,0 +208,0 @@ const obj = JSON.parse(arg); |
{ | ||
"name": "tiny-lru", | ||
"description": "Tiny LRU cache for Client or Server", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/avoidwork/tiny-lru", | ||
@@ -21,2 +21,3 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>", | ||
"scripts": { | ||
"benchmark": "node benchmark.js", | ||
"test": "grunt test" | ||
@@ -33,3 +34,4 @@ }, | ||
"grunt-contrib-watch": "^1.1.0", | ||
"grunt-eslint": "^21.0.0" | ||
"grunt-eslint": "^21.0.0", | ||
"precise": "^1.1.0" | ||
}, | ||
@@ -36,0 +38,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ # Tiny LRU | ||
```javascript | ||
const cache = lru(max [, notify = false, ttl = 0, expire = 0]); | ||
const cache = lru(max, notify = false, ttl = 0); | ||
``` | ||
@@ -48,15 +48,2 @@ | ||
## expire | ||
### Property | ||
Milliseconds an item will remain in cache, does not reset when accessed | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.expire = 6e4; | ||
``` | ||
## first | ||
@@ -89,15 +76,2 @@ ### Property | ||
## items | ||
### Property | ||
Hash of cache items | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.items; // {} | ||
``` | ||
## max | ||
@@ -219,3 +193,3 @@ ### Property | ||
Milliseconds an item will remain in cache, resets when accessed | ||
Milliseconds an item will remain in cache; lazy expiration upon next `get()` of an item | ||
@@ -222,0 +196,0 @@ **Example** |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
0
9980
9
5
196
203