Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lru

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lru - npm Package Compare versions

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 () {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc