From 4a385dd9382df0d02b1a72aa7d39910975b06c21 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Wed, 25 Feb 2015 06:37:48 -0800 Subject: [PATCH] * Improved code coverage in tests * Refactored timeout storage/handling * Removed expired checks (let timeouts handle it) * Doc updates --- README.md | 7 +++++++ index.js | 57 ++++++++++++++++++++++----------------------------- package.json | 2 +- test/index.js | 3 +++ 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index f9799cf..0ff2de6 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,13 @@ The number of cache misses. Many thanks to Paul Tarjan for the first iteration of this library (https://github.com/ptarjan/node-cache). ## CHANGELOG +1.0.1 +----- +* Improved code coverage in tests +* Refactored timeout storage/handling +* Removed expired checks (let timeouts handle it) +* Doc updates + 1.0.0 ----- * Change size, memsize, hits and misses to getters (breaking change) diff --git a/index.js b/index.js index 9948a79..290b696 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,10 @@ var sizeof = require( 'js-sizeof' ); -function expired( record ) { - return record.expires && record.expires < +new Date(); -} - var TinyCache = function() { var self = this; - self.cache = {}; + self._cache = {}; + self._timeouts = {}; self._hits = 0; self._misses = 0; self._size = 0; @@ -21,7 +18,7 @@ TinyCache.prototype = { return this._size; }, get memsize() { - return sizeof( this.cache ); /* Returns the approximate memory usage of all objects stored in the cache and cache overhead */ + return sizeof( this._cache ); /* Returns the approximate memory usage of all objects stored in the cache and cache overhead */ }, get hits() { return this._hits; @@ -34,63 +31,57 @@ TinyCache.prototype = { TinyCache.prototype.put = function( key, value, time ) { var self = this; - if ( self.cache[ key ] ) { - clearTimeout( self.cache[ key ].timeout ); + if ( self._timeouts[ key ] ) { + clearTimeout( self._timeouts[ key ] ); + delete self._timeouts[ key ]; } - self.cache[ key ] = { - value: value, - expires: !isNaN( time ) ? ( time + new Date() ) : null, - timeout: !isNaN( time ) ? setTimeout( self.del.bind( self, key ), time ) : null - }; + self._cache[ key ] = value; + + if ( !isNaN( time ) ) { + self._timeouts[ key ] = setTimeout( self.del.bind( self, key ), time ); + } ++self._size; }; TinyCache.prototype.del = function( key ) { var self = this; - var record = self.cache[ key ]; - if ( !record ) { + clearTimeout( self._timeouts[ key ] ); + delete self._timeouts[ key ]; + + if ( !( key in self._cache ) ) { return false; } - - clearTimeout( record.timeout ); - - var isExpired = expired( record ); - delete self.cache[ key ]; + + delete self._cache[ key ]; --self._size; - return !isExpired; + return true; }; TinyCache.prototype.clear = function() { var self = this; - for ( var key in self.cache ) { - clearTimeout( self.cache[ key ].timeout ); + for ( var key in self._timeouts ) { + clearTimeout( self._timeouts[ key ] ); } - self.cache = {}; + self._cache = {}; + self._timeouts = {}; self._size = 0; }; TinyCache.prototype.get = function( key ) { var self = this; - var record = self.cache[ key ]; - if ( !record ) { - ++self._misses; - return null; - } - - if ( expired( record ) ) { + if ( !( key in self._cache ) ) { ++self._misses; - self.del( key ); return null; } ++self._hits; - return record.value; + return self._cache[ key ]; }; TinyCache.shared = new TinyCache(); diff --git a/package.json b/package.json index c6f561d..c16d7f2 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "storage" ], "main": "./index.js", - "version": "1.0.0", + "version": "1.0.1", "scripts": { "test": "mocha -R list" }, diff --git a/test/index.js b/test/index.js index 5a63310..54e11bc 100644 --- a/test/index.js +++ b/test/index.js @@ -170,6 +170,9 @@ var suite = function( cache ) { assert.isNull( cache.get( key ) ); assert.equal( cache.size, size ); } ); + it( 'should return false when trying to remove a nonexistent object', function() { + assert.notOk( cache.del( 'nonexistent' ) ); + } ); } ); describe( 'hits', function() {