Comparing version 0.0.3 to 0.0.4
56
index.js
@@ -7,6 +7,6 @@ var events = require("events"); | ||
* A timeout will be added to each entry. | ||
* When a timeout happens for a single entry, the event 'expired' | ||
* When a timeout happens for a single entry, the event 'expired' | ||
* will be rised with the pair key/value as argument. | ||
* When an entry was query or updated, its timeout will be reseted. | ||
* @param options {object} Optional configuration options. | ||
* @param options {object} Optional configuration options. | ||
* - timeout {number} Optional. Specifies in ms the default timeout for each entry. Default 60000 ms. | ||
@@ -28,3 +28,3 @@ * - doesNotRenewTimeout {boolean} Optional. Specifies if entries's timeout should be reseted after each query or update. Default false. | ||
var _length = 0; // Internal counter of cache's entries | ||
var expirations = []; // Entries by sorted ascending by expiration time nad sequence. | ||
var expirations = []; // Entries sorted ascending by their expiration time and sequence. | ||
var sequence = 0; // Internal counter for sorting entries within 'expirations' array. | ||
@@ -38,6 +38,6 @@ var timerId = null; // Reference to timer | ||
/* | ||
/* | ||
* Returns the number of entries that the cache contains. | ||
* @api public | ||
*/ | ||
*/ | ||
Object.defineProperty(this, "length", { | ||
@@ -49,6 +49,6 @@ enumerable: true, | ||
/* | ||
/* | ||
* Returns all keys. | ||
* @api public | ||
*/ | ||
*/ | ||
Object.defineProperty(this, "keys", { | ||
@@ -61,7 +61,7 @@ enumerable: true, | ||
* Inserts or updates an entry into the cache. | ||
* @param key {string} Required. | ||
* @param key {string} Required. | ||
* @param value {any} Required. | ||
* @param timeout {number} Optional. Specifies in milliseconds the timeout for this entry. | ||
* @api public | ||
*/ | ||
*/ | ||
this.set = function (key, value, timeout) { | ||
@@ -91,8 +91,8 @@ var current = cache[key]; | ||
* Removes an entry from the cache. | ||
* @param key {string} Required. | ||
* @param key {string} Required. | ||
* @api public | ||
*/ | ||
*/ | ||
this.remove =function (key) { | ||
var item = cache[key]; | ||
if (!item) return null; | ||
if (!item) return null; | ||
@@ -107,6 +107,6 @@ _length --; | ||
* Gets an entry's value by its key. | ||
* @param key {string} Required. | ||
* @param key {string} Required. | ||
* @return {any} Returns entry's value or null if entry was not found | ||
* @api public | ||
*/ | ||
*/ | ||
this.get = function (key) { | ||
@@ -122,3 +122,3 @@ var item = cache[key]; | ||
return null; | ||
} | ||
}; | ||
@@ -138,3 +138,3 @@ /* | ||
_length = 0; | ||
} | ||
}; | ||
@@ -188,3 +188,3 @@ // adds an entry to expirations array | ||
if (item.expires > now) { | ||
// Sets timer for no expired item | ||
@@ -197,8 +197,12 @@ setItemTimeout(item); | ||
} | ||
// All remaining expirations may need to be removed... | ||
else if( index == expirations.length-1 && item.expires <= now) { | ||
expirations = expirations.slice(index+1); | ||
} | ||
// Adds expired entry to collection of expired items | ||
itemsToEmit.push(item); | ||
itemsToEmit.push(item); | ||
// Removes expired entry from cache | ||
delete cache[item.key]; | ||
delete cache[item.key]; | ||
} | ||
@@ -211,8 +215,8 @@ | ||
itemsToEmit.forEach( function( item ) { | ||
self.emit("expired", { | ||
self.emit("expired", { | ||
key: item.key, | ||
value: item.value | ||
value: item.value | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -225,3 +229,3 @@ // Internal function that compares two entries's timeouts | ||
return a.sequence - b.sequence; | ||
}; | ||
} | ||
@@ -236,3 +240,3 @@ return a.expires - b.expires; | ||
while ( low <= up ) { | ||
middle = (low + up) >> 1; | ||
@@ -258,3 +262,3 @@ result = itemComparer(value, expirations[middle]); | ||
while ( low <= up ) { | ||
middle = (low + up) >> 1; | ||
@@ -276,3 +280,3 @@ result = itemComparer(value, expirations[middle]); | ||
} | ||
}; | ||
@@ -279,0 +283,0 @@ // Cache inherits from EventEmitter |
{ | ||
"name": "mem-cache", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "simple in memory key/value cache with autoclean by timeout", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
], | ||
"author": "Silvio Massari", | ||
"author": "Silvio Massari <silvio.massari@gmail.com>", | ||
"license": "MIT", | ||
@@ -19,0 +19,0 @@ "dependencies": { |
@@ -0,0 +0,0 @@ # node-mem-cache |
@@ -45,3 +45,3 @@ var assert = require("assert"); | ||
assert.equal(5, cache.length); | ||
cache.clean(); | ||
@@ -61,3 +61,3 @@ done(); | ||
assert.equal(null, value); | ||
cache.clean(); | ||
@@ -82,3 +82,3 @@ done(); | ||
assert.equal(5, cache.length); | ||
cache.clean(); | ||
@@ -88,4 +88,2 @@ done(); | ||
it ("should be able to remove a value by its key", function (done) { | ||
@@ -103,3 +101,3 @@ | ||
assert.equal(4, cache.length); | ||
cache.clean(); | ||
@@ -109,2 +107,3 @@ done(); | ||
it ("should expire an item by timeout", function (done) { | ||
@@ -114,3 +113,3 @@ | ||
var cache = new Cache(); | ||
var cache = new Cache(); | ||
cache.set("foo0", "bar0"); | ||
@@ -128,4 +127,4 @@ cache.set("foo1", "bar1"); | ||
assert.equal("bar2", item.value); | ||
; | ||
var delta =new Date().getTime() - now; | ||
var delta =new Date().getTime() - now; | ||
assert.ok( timeout < delta); | ||
@@ -136,3 +135,3 @@ assert.ok( timeout*2 > delta); | ||
assert.equal(null, cache.get("foo2")); | ||
cache.clean(); | ||
@@ -143,5 +142,56 @@ done(); | ||
it ("should expire new items added after expiring all items in cache", function (done) { | ||
var expiredEventCount=0; | ||
var now = new Date().getTime(); | ||
var cache = new Cache(); | ||
var timeout = 100; | ||
cache.set("foo0", "bar0", timeout); | ||
cache.set("foo1", "bar1", timeout); | ||
assert.equal(2, cache.length); | ||
cache.on("expired", function (item) { | ||
expiredEventCount++; | ||
assert.ok(item.key === "foo0" || item.key == "foo1"); | ||
if(item.key === "foo0") { | ||
assert.equal("bar0", item.value); | ||
assert.equal(null, cache.get("foo0")); | ||
} else if (item.key === "foo1") { | ||
assert.equal("bar1", item.value); | ||
assert.equal(null, cache.get("foo1")); | ||
} | ||
// On second event - add a new item, and wait for it to expire | ||
// (it won't without Stuart's bugfix) | ||
if(expiredEventCount == 2) { | ||
cache.removeAllListeners("expired"); // remove this callback | ||
cache.set("foo2", "bar2", timeout); | ||
assert.equal(1, cache.length); | ||
cache.on("expired", function (newitem) { | ||
expiredEventCount++; | ||
assert.equal("foo2", newitem.key); | ||
assert.equal("bar2", newitem.value); | ||
assert.equal(0, cache.length); | ||
assert.equal(null, cache.get("foo2")); | ||
assert.equal(3, expiredEventCount); | ||
cache.clean(); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
it ("should not expire an item if timeouts are disabled", function (done) { | ||
var cache = new Cache({ timeoutDisabled: true }); | ||
cache.on("expired", function(item) { | ||
@@ -158,9 +208,9 @@ throw new Error("Should not expire items!!"); | ||
setTimeout( function() { | ||
var value = cache.get("foo2"); | ||
assert.equal("bar2", value); | ||
cache.clean(); | ||
done(); | ||
}, timeout * 2); | ||
@@ -174,3 +224,3 @@ }); | ||
var cache = new Cache({ doesNotRenewTimeout: false }); | ||
var cache = new Cache({ doesNotRenewTimeout: false }); | ||
cache.set("foo0", "bar0"); | ||
@@ -183,11 +233,11 @@ cache.set("foo1", "bar1"); | ||
setTimeout( function() { | ||
var value = cache.get("foo2"); | ||
assert.equal("bar2", value); | ||
setTimeout( function() { | ||
var value = cache.get("foo2"); | ||
assert.equal("bar2", value); | ||
}, timeout * 2/3); | ||
@@ -202,3 +252,3 @@ }, timeout * 2/3); | ||
assert.equal(2, cache.length); | ||
cache.clean(); | ||
@@ -211,3 +261,3 @@ done(); | ||
it ("should be able to clean the cache", function (done) { | ||
var cache = new Cache(); | ||
var cache = new Cache(); | ||
cache.set("foo0", "bar0"); | ||
@@ -222,6 +272,6 @@ cache.set("foo1", "bar1"); | ||
it ("should be able to get all keys", function (done) { | ||
var cache = new Cache(); | ||
var cache = new Cache(); | ||
cache.set("foo0", "bar0"); | ||
cache.set("foo1", "bar1"); | ||
var keys = cache.keys; | ||
@@ -228,0 +278,0 @@ assert.equal(2, keys.length); |
17235
5
407