diff --git a/README.md b/README.md index 270ea81..d1dd8c1 100644 --- a/README.md +++ b/README.md @@ -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 ======== @@ -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 =============== @@ -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 diff --git a/lib/lrucache.js b/lib/lrucache.js index 8e345c4..3b42e47 100644 --- a/lib/lrucache.js +++ b/lib/lrucache.js @@ -64,6 +64,11 @@ LruCache.prototype = { }, del: 'set below', + + keys: + function keys( ) { + return Object.keys(this.keyvals); + }, }; LruCache.prototype.del = LruCache.prototype.delete; diff --git a/lib/lrucache2.js b/lib/lrucache2.js index 0f47700..b3222a2 100644 --- a/lib/lrucache2.js +++ b/lib/lrucache2.js @@ -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 } diff --git a/lib/mvcache.js b/lib/mvcache.js index 77047b6..5a3e2bf 100644 --- a/lib/mvcache.js +++ b/lib/mvcache.js @@ -77,6 +77,11 @@ MultiValueCache.prototype = { } }, + keys: + function keys( ) { + return Object.keys(this._lists); + }, + _deleted: null, _deletedCount: 0, _delayedDelete: diff --git a/lib/qcache.js b/lib/qcache.js index 4b7c90e..6971a3e 100644 --- a/lib/qcache.js +++ b/lib/qcache.js @@ -69,6 +69,11 @@ TtlCache.prototype = { } }, + keys: + function keys( ) { + return Object.keys(this._store); + }, + gc: function gc( ) { var ts = this.getTimestamp(); diff --git a/package.json b/package.json index d7b73cb..b65222f 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -28,7 +28,7 @@ "qtimebase": "1.0.0" }, "devDependencies": { - "qnit": "0.12.0" + "qnit": "0.25" }, "scripts": { "test": "qnit", diff --git a/test/test-lrucache.js b/test/test-lrucache.js index b92e9b0..eaccd4f 100644 --- a/test/test-lrucache.js +++ b/test/test-lrucache.js @@ -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); diff --git a/test/test-mvcache.js b/test/test-mvcache.js index c22c14a..e94a602 100644 --- a/test/test-mvcache.js +++ b/test/test-mvcache.js @@ -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); diff --git a/test/test-qcache.js b/test/test-qcache.js index 3bd0cce..73a8c5d 100644 --- a/test/test-qcache.js +++ b/test/test-qcache.js @@ -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();