lru-cache
Advanced tools
Comparing version 2.2.4 to 2.3.0
@@ -196,12 +196,27 @@ ;(function () { // closure for web browsers | ||
this.get = function (key) { | ||
if (!hOP(cache, key)) return | ||
return get(key, true) | ||
} | ||
this.peek = function (key) { | ||
return get(key, false) | ||
} | ||
function get (key, doUse) { | ||
var hit = cache[key] | ||
if (maxAge && (Date.now() - hit.now > maxAge)) { | ||
this.del(key) | ||
return allowStale ? hit.value : undefined | ||
if (hit) { | ||
if (maxAge && (Date.now() - hit.now > maxAge)) { | ||
del(hit) | ||
if (!allowStale) hit = undefined | ||
} else { | ||
if (doUse) use(hit) | ||
} | ||
if (hit) hit = hit.value | ||
} | ||
return hit | ||
} | ||
function use (hit) { | ||
shiftLU(hit) | ||
hit.lu = mru ++ | ||
lruList[hit.lu] = hit | ||
return hit.value | ||
} | ||
@@ -208,0 +223,0 @@ |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -63,2 +63,11 @@ # lru cache | ||
* `peek(key)` | ||
Returns the key value (or `undefined` if not found) without | ||
updating the "recently used"-ness of the key. | ||
(If you find yourself using this a lot, you *might* be using the | ||
wrong sort of data structure, but there are some use cases where | ||
it's handy.) | ||
* `del(key)` | ||
@@ -65,0 +74,0 @@ |
@@ -318,1 +318,13 @@ var test = require("tap").test | ||
}) | ||
test("least recently set w/ peek", function (t) { | ||
var cache = new LRU(2) | ||
cache.set("a", "A") | ||
cache.set("b", "B") | ||
t.equal(cache.peek("a"), "A") | ||
cache.set("c", "C") | ||
t.equal(cache.get("c"), "C") | ||
t.equal(cache.get("b"), "B") | ||
t.equal(cache.get("a"), undefined) | ||
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
20399
600
98