Comparing version 0.1.3 to 0.1.4
100
index.js
@@ -0,2 +1,6 @@ | ||
var events = require('events'); | ||
var util = require('util'); | ||
var LRU = exports.LRU = function (max) { | ||
events.EventEmitter.call(this); | ||
this.cache = {} | ||
@@ -7,23 +11,39 @@ this.head = this.tail = null; | ||
}; | ||
util.inherits(LRU, events.EventEmitter); | ||
LRU.prototype.remove = function (key) { | ||
var element = this.cache[key]; | ||
if(element) { | ||
delete this.cache[key]; | ||
--this.length; | ||
if(element.prev) this.cache[element.prev].next = element.next; | ||
if(element.next) this.cache[element.next].prev = element.prev; | ||
if(this.head == key) { | ||
this.head = element.prev; | ||
} | ||
if(this.tail == key) { | ||
this.tail = element.next; | ||
} | ||
} | ||
return element; | ||
} | ||
LRU.prototype.set = function (key, value) { | ||
key += ''; | ||
if (key in this.cache) { | ||
return this.cache[key].value = value; | ||
} | ||
this.cache[key] = { | ||
next: null | ||
, prev: this.head | ||
, value: value | ||
}; | ||
element = this.remove(key); | ||
element = element || { value:value }; | ||
element.next = null; | ||
element.prev = this.head; | ||
this.cache[key] = element; | ||
if (this.head) { | ||
if (!(this.head in this.cache)) { | ||
return this.reset(); | ||
} | ||
this.cache[this.head].next = key; | ||
} | ||
this.head = key; | ||
if (!this.tail) { | ||
this.tail = key; | ||
if(!this.tail) { | ||
this.tail = key; | ||
} | ||
if (++this.length > this.max) { | ||
@@ -35,33 +55,6 @@ this.evict(); | ||
LRU.prototype.get = function (key) { | ||
key += ''; | ||
var element = this.cache[key]; | ||
if (!element) { | ||
return; | ||
} | ||
if (this.head === key) { | ||
return element.value; | ||
} | ||
if (element.next) { | ||
if (!(element.next in this.cache)) { | ||
return this.reset(); | ||
} | ||
this.cache[element.next].prev = element.prev; | ||
} | ||
if (element.prev) { | ||
if (!(element.prev in this.cache)) { | ||
return this.reset(); | ||
} | ||
this.cache[element.prev].next = element.next; | ||
} else { | ||
this.tail = element.next || key; | ||
} | ||
element.prev = this.head; | ||
element.next = null; | ||
if (this.head) { | ||
if (!(this.head in this.cache)) { | ||
return this.reset(); | ||
} | ||
this.cache[this.head].next = key; | ||
} | ||
this.head = key; | ||
if (!element) { return; } | ||
this.set(key, element.value); | ||
return element.value; | ||
@@ -71,18 +64,7 @@ }; | ||
LRU.prototype.evict = function () { | ||
var tail = this.cache[this.tail].next; | ||
delete this.cache[this.tail]; | ||
if (!(tail in this.cache)) { | ||
return this.reset(); | ||
} | ||
this.cache[tail].prev = null; | ||
this.tail = tail; | ||
this.length--; | ||
if(!this.tail) { return; } | ||
var key = this.tail; | ||
var element = this.remove(this.tail); | ||
this.emit('evict', {key:key, value:element.value}); | ||
}; | ||
LRU.prototype.reset = function () { | ||
//Super shit but this sometimes (rarely) fails | ||
this.cache = {}; | ||
this.length = 0; | ||
this.head = this.tail = null; | ||
}; | ||
{ "name" : "lru", | ||
"description" : "A simple O(1) LRU cache", | ||
"version" : "0.1.3", | ||
"version" : "0.1.4", | ||
"author" : "Chris O'Hara <cohara87@gmail.com>", | ||
@@ -9,2 +9,5 @@ "main" : "index", | ||
}, | ||
"homepage": { | ||
"url": "http://github.com/chriso/lru" | ||
}, | ||
"repository": { | ||
@@ -17,3 +20,6 @@ "type": "git", | ||
"type": "MIT" | ||
}] | ||
}], | ||
"devDependencies": { | ||
"vows": "~0.6" | ||
} | ||
} |
@@ -20,1 +20,6 @@ **A simple LRU cache supporting O(1) set, get and eviction of old keys** | ||
### Credits | ||
A big thanks to [Dusty Leary](https://github.com/dustyleary) who | ||
finished the library. | ||
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
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
6845
6
167
1
25
1
1