Socket
Socket
Sign inDemoInstall

lru

Package Overview
Dependencies
Maintainers
1
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 1.1.0 to 1.2.0

16

index.js
var events = require('events');
var util = require('util');
var LRU = module.exports = function (max) {
if (!(this instanceof LRU)) return new LRU(max);
var LRU = module.exports = function (opts) {
if (!(this instanceof LRU)) return new LRU(opts);
if (typeof opts === 'number') opts = {max: opts};
if (!opts) opts = {};
events.EventEmitter.call(this);

@@ -10,3 +12,4 @@ this.cache = {}

this.length = 0;
this.max = max || 1000;
this.max = opts.max || 1000;
this.maxAge = opts.maxAge || 0;
};

@@ -45,2 +48,3 @@ util.inherits(LRU, events.EventEmitter);

element.value = value
if (this.maxAge) element.modified = Date.now();

@@ -53,2 +57,3 @@ // If it's already the head, there's nothing more to do:

element = { value:value };
if (this.maxAge) element.modified = Date.now();

@@ -81,2 +86,7 @@ this.cache[key] = element;

if (!element) { return; }
if (this.maxAge && (Date.now() - element.modified) > this.maxAge) {
this.remove(key);
this.emit('evict', {key:key, value:element.value});
return;
}

@@ -83,0 +93,0 @@ if( this.length > 1 ) {

2

package.json
{
"name": "lru",
"description": "A simple O(1) LRU cache",
"version": "1.1.0",
"version": "1.2.0",
"author": "Chris O'Hara <cohara87@gmail.com>",

@@ -6,0 +6,0 @@ "main": "index",

@@ -38,3 +38,13 @@ # lru

Create a new LRU cache that stores `length` elements before evicting the least recently used.
Optionally you can pass a an options map with additional options instead
``` js
{
max: maxElementsToStore,
maxAge: maxAgeInMilliseconds
}
```
If you pass `maxAge` items will be evicted if they are older than `maxAge` when you access them.
**Returns**: the newly created LRU cache

@@ -41,0 +51,0 @@

@@ -130,2 +130,33 @@ var assert = require('assert');

suite.addBatch({
"evicting items by 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.get('foo'), null);
},
},
"evicting items by age (2)": {
topic: function () {
var lru = new LRU({maxAge: 100000});
lru.set('foo', 'bar');
assert.equal(lru.get('foo'), 'bar');
var callback = this.callback
setTimeout(function () {
callback(null, lru);
}, 100);
},
"the entry is not removed if age < max_age": function(lru) {
assert.equal(lru.get('foo'), 'bar');
}
}
});
suite.addBatch({
"idempotent 'changes'": {

@@ -143,3 +174,2 @@ "set() and remove() on empty LRU is idempotent": function() {

"2 set()s and 2 remove()s on empty LRU is idempotent": function() {

@@ -146,0 +176,0 @@ var lru = new LRU();

Sorry, the diff of this file is not supported yet

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