Skip to content

Commit

Permalink
new keys() method; v 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Feb 11, 2019
1 parent ab7474b commit 8b85b1f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ remove the value from the cache. Removed items read as undefined.

remove expired elements from the cache to free the occupied storage space

### keys( )

return the currently tracked cache keys, including keys that may be expired or been deleted.


LruCache
========
Expand Down Expand Up @@ -142,6 +146,10 @@ Missing keys and displaced values read as `undefined`.

remove the key from the cache.

### keys( )

return the currently tracked cache keys, including keys that may be expired or been deleted.


MultiValueCache
===============
Expand Down Expand Up @@ -182,10 +190,15 @@ test whether there are any values stored under key

return the count of items stored under `key`

### keys( )

return the currently tracked cache keys, including keys that may be expired or been deleted.


Change Log
----------

- 0.8.0 - `keys` method on TtlCache, LruCache, MultiValueCache, test with newer `qnit`
- 0.7.2 - faster LruCache
- 0.7.1 - LruCache much faster rewrite, fix LruCache1 count
- 0.6.2 - improve lrucache tests
Expand Down
5 changes: 5 additions & 0 deletions lib/lrucache.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ LruCache.prototype = {
},

del: 'set below',

keys:
function keys( ) {
return Object.keys(this.keyvals);
},
};

LruCache.prototype.del = LruCache.prototype.delete;
Expand Down
7 changes: 7 additions & 0 deletions lib/lrucache2.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ LruCache.prototype.del = LruCache.prototype.delete = function delete_( key ) {
this.count -= 1;
}
}

LruCache.prototype.keys = function keys( ) {
return Object.keys(this.nodemap);
}

LruCache.prototype = toStruct(LruCache.prototype);
function toStruct(hash) { return toStruct.prototype = hash }
5 changes: 5 additions & 0 deletions lib/mvcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ MultiValueCache.prototype = {
}
},

keys:
function keys( ) {
return Object.keys(this._lists);
},

_deleted: null,
_deletedCount: 0,
_delayedDelete:
Expand Down
5 changes: 5 additions & 0 deletions lib/qcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ TtlCache.prototype = {
}
},

keys:
function keys( ) {
return Object.keys(this._store);
},

gc:
function gc( ) {
var ts = this.getTimestamp();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qcache",
"version": "0.7.2",
"version": "0.8.0",
"description": "useful caches: ttl, lru, multi-value",
"license": "Apache-2.0",
"repository": {
Expand Down Expand Up @@ -28,7 +28,7 @@
"qtimebase": "1.0.0"
},
"devDependencies": {
"qnit": "0.12.0"
"qnit": "0.25"
},
"scripts": {
"test": "qnit",
Expand Down
9 changes: 9 additions & 0 deletions test/test-lrucache.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ module.exports = {
},
},

'keys': {
'should return cache keys': function(t) {
this.cache.set('a', 1);
this.cache.set('b', 2);
t.deepEqual(this.cache.keys(), ['a', 'b']);
t.done();
},
},

'should track count': function(t) {
var cache = new LruCache();
t.equal(cache.count, 0);
Expand Down
8 changes: 8 additions & 0 deletions test/test-mvcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ module.exports = {
t.done();
},

'should return list keys': function(t) {
this.cache.push('t', 1);
this.cache.push('t', 2);
this.cache.push('tt', 3);
t.deepEqual(this.cache.keys(), ['t', 'tt']);
t.done();
},

'should periodically compact removed names': function(t) {
for (var i=0; i<50; i++) {
this.cache.push(i, i);
Expand Down
8 changes: 8 additions & 0 deletions test/test-qcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ module.exports = {
}, 22);
},

'keys should return cache keys': function(t) {
var cache = new TtlCache();
cache.set('key1', 1);
cache.set('key22', 22);
t.deepEqual(cache.keys(), ['key1', 'key22']);
t.done();
},

'time 200k set/get calls': function(t) {
var x;
var t1 = Date.now();
Expand Down

0 comments on commit 8b85b1f

Please sign in to comment.