Skip to content

Commit

Permalink
* Improved code coverage in tests
Browse files Browse the repository at this point in the history
* Refactored timeout storage/handling
* Removed expired checks (let timeouts handle it)
* Doc updates
  • Loading branch information
andyburke committed Feb 25, 2015
1 parent cf0af95 commit 4a385dd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down
57 changes: 24 additions & 33 deletions index.js
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
"storage"
],
"main": "./index.js",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"test": "mocha -R list"
},
Expand Down
3 changes: 3 additions & 0 deletions test/index.js
Expand Up @@ -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() {
Expand Down

0 comments on commit 4a385dd

Please sign in to comment.