Comparing version 2.0.1 to 3.0.0
26
index.js
@@ -20,2 +20,6 @@ var events = require('events') | ||
Object.defineProperty(LRU.prototype, 'keys', { | ||
get: function () { return Object.keys(this.cache) } | ||
}) | ||
LRU.prototype.remove = function (key) { | ||
@@ -51,3 +55,8 @@ if (typeof key !== 'string') key = '' + key | ||
LRU.prototype.peek = function (key) { | ||
return this.cache.hasOwnProperty(key) ? this.cache[key].value : null | ||
if (!this.cache.hasOwnProperty(key)) return | ||
var element = this.cache[key] | ||
if (!this._checkAge(key, element)) return | ||
return element.value | ||
} | ||
@@ -88,2 +97,11 @@ | ||
LRU.prototype._checkAge = function (key, element) { | ||
if (this.maxAge && (Date.now() - element.modified) > this.maxAge) { | ||
this.remove(key) | ||
this.emit('evict', {key: key, value: element.value}) | ||
return false | ||
} | ||
return true | ||
} | ||
LRU.prototype.get = function (key) { | ||
@@ -95,7 +113,3 @@ if (typeof key !== 'string') key = '' + key | ||
if (this.maxAge && (Date.now() - element.modified) > this.maxAge) { | ||
this.remove(key) | ||
this.emit('evict', {key: key, value: element.value}) | ||
return | ||
} | ||
if (!this._checkAge(key, element)) return | ||
@@ -102,0 +116,0 @@ if (this.head !== key) { |
{ | ||
"name": "lru", | ||
"description": "A simple O(1) LRU cache", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"author": "Chris O'Hara <cohara87@gmail.com>", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -56,2 +56,5 @@ # lru | ||
##### `.keys` | ||
Array of all the keys currently in the cache. | ||
#### Methods | ||
@@ -58,0 +61,0 @@ |
@@ -5,12 +5,2 @@ var assert = require('assert') | ||
function keys (obj) { | ||
var result = [] | ||
for (var k in obj) { | ||
if (obj.hasOwnProperty(k)) { | ||
result.push(k) | ||
} | ||
} | ||
return result | ||
} | ||
var suite = vows.describe('LRU') | ||
@@ -55,3 +45,3 @@ | ||
assert.deepEqual(['foo3', 'foo4'], keys(lru.cache)) | ||
assert.deepEqual(['foo3', 'foo4'], lru.keys) | ||
} | ||
@@ -81,3 +71,3 @@ }) | ||
assert.deepEqual(['foo1', 'foo3'], keys(lru.cache)) | ||
assert.deepEqual(['foo1', 'foo3'], lru.keys) | ||
}, | ||
@@ -92,3 +82,3 @@ 'lru invariant is maintained after set(), get() and remove()': function () { | ||
lru.set('d', 1) | ||
assert.deepEqual(['c', 'd'], keys(lru.cache)) | ||
assert.deepEqual(['c', 'd'], lru.keys) | ||
} | ||
@@ -98,14 +88,2 @@ }) | ||
suite.addBatch({ | ||
'lru invariant is maintained for get()': function () { | ||
var lru = new LRU(2) | ||
lru.set('foo1', 'bar1') | ||
lru.set('foo2', 'bar2') | ||
lru.get('foo2') // now foo2 should be deleted instead of foo1 | ||
lru.set('foo3', 'bar3') | ||
assert.deepEqual(['foo2', 'foo3'], keys(lru.cache)) | ||
}, | ||
'lru invariant is maintained in the corner case size == 1': function () { | ||
@@ -121,3 +99,3 @@ var lru = new LRU(1) | ||
assert.deepEqual(['foo3'], keys(lru.cache)) | ||
assert.deepEqual(['foo3'], lru.keys) | ||
} | ||
@@ -146,2 +124,19 @@ }) | ||
suite.addBatch({ | ||
'peek respects max age': { | ||
topic: function () { | ||
var lru = new LRU({maxAge: 5}) | ||
lru.set('foo', 'bar') | ||
assert.equal(lru.get('foo'), 'bar') | ||
var callback = this.callback | ||
setTimeout(function () { | ||
callback(null, lru) | ||
}, 100) | ||
}, | ||
'the entry is removed if age > max_age': function (lru) { | ||
assert.equal(lru.peek('foo'), null) | ||
} | ||
} | ||
}) | ||
suite.addBatch({ | ||
'evicting items by age': { | ||
@@ -148,0 +143,0 @@ topic: function () { |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13835
332
93
0