Skip to content

Commit

Permalink
Clean up set method
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 15, 2015
1 parent 1763ce3 commit 356082a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ LRUCache.prototype.dumpLru = function () {
}

LRUCache.prototype.set = function (key, value, maxAge) {
maxAge = maxAge || this._maxAge
var now = maxAge ? Date.now() : 0

if (hOP(this._cache, key)) {
// dispose of the old one before overwriting
if (this._dispose) this._dispose(key, this._cache[key].value)
if (maxAge || this._maxAge) this._cache[key].now = Date.now()
delete this._cache[key]['maxAge']
if (maxAge) this._cache[key].maxAge = maxAge
if (this._dispose)
this._dispose(key, this._cache[key].value)

this._cache[key].now = now
this._cache[key].maxAge = maxAge
this._cache[key].value = value
this.get(key)
return true
}

var len = this._lengthCalculator(value)
var now = (maxAge || this._maxAge) ? Date.now() : 0
var maxAge = maxAge || this._maxAge
var hit = new Entry(key, value, this._mru++, len, now, maxAge)

// oversized objects fall out of cache automatically.
Expand All @@ -174,7 +176,9 @@ LRUCache.prototype.set = function (key, value, maxAge) {
this._lruList[hit.lu] = this._cache[key] = hit
this._itemCount ++

if (this._length > this._max) trim(this)
if (this._length > this._max)
trim(this)

return true
}

Expand Down

0 comments on commit 356082a

Please sign in to comment.