diff options
Diffstat (limited to 'node_modules/lru-cache/lib/lru-cache.js')
-rw-r--r-- | node_modules/lru-cache/lib/lru-cache.js | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/node_modules/lru-cache/lib/lru-cache.js b/node_modules/lru-cache/lib/lru-cache.js index 1bd4e5864..407ee6857 100644 --- a/node_modules/lru-cache/lib/lru-cache.js +++ b/node_modules/lru-cache/lib/lru-cache.js @@ -4,7 +4,7 @@ if (module) { module.exports = LRUCache } else { // just set the global for non-node platforms. - ;(function () { return this })().LRUCache = LRUCache + this.LRUCache = LRUCache } function hOP (obj, key) { @@ -13,7 +13,7 @@ function hOP (obj, key) { function naiveLength () { return 1 } -function LRUCache (maxLength, lengthCalculator) { +function LRUCache (maxLength, lengthCalculator, maxAge) { if (!(this instanceof LRUCache)) { return new LRUCache(maxLength, lengthCalculator) } @@ -101,24 +101,30 @@ function LRUCache (maxLength, lengthCalculator) { if (hOP(cache, key)) { this.get(key) cache[key].value = value - return + return true } var hit = {key:key, value:value, lu:mru++, length:lengthCalculator(value)} + if (maxAge) hit.now = Date.now() // oversized objects fall out of cache automatically. - if (hit.length > maxLength) return + if (hit.length > maxLength) return false length += hit.length lruList[hit.lu] = cache[key] = hit itemCount ++ if (length > maxLength) trim() + return true } this.get = function (key) { if (!hOP(cache, key)) return var hit = cache[key] + if (maxAge && Date.now() - hit.now > maxAge) { + this.del(key) + return + } delete lruList[hit.lu] if (hit.lu === lru) lruWalk() hit.lu = mru ++ @@ -149,6 +155,7 @@ function LRUCache (maxLength, lengthCalculator) { delete cache[ lruList[prune[i]].key ] delete lruList[prune[i]] } + lruWalk() } } |