From fb887656e84c0b28e9191f22b418ccc9c9ae4aad Mon Sep 17 00:00:00 2001 From: ccrowhurstram Date: Wed, 21 Oct 2015 21:59:59 +0100 Subject: [PATCH] feat(CacheFactory): new dispose method Calling `cache.dispose` will dispose of that cache removing it from the `CacheFactory`; any items that the cache has added to underlying storage will *not* be removed --- src/index.js | 15 +++++++++++-- test/unit/Cache/index.dispose.test.js | 31 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 test/unit/Cache/index.dispose.test.js diff --git a/src/index.js b/src/index.js index d95edf4..cf3ca86 100644 --- a/src/index.js +++ b/src/index.js @@ -98,13 +98,17 @@ let createCache = (cacheId, options) => { $$id: cacheId, destroy() { - clearInterval(this.$$cacheFlushIntervalId); - clearInterval(this.$$recycleFreqId); this.removeAll(); if ($$storage) { $$storage().removeItem(`${this.$$prefix}.keys`); $$storage().removeItem(this.$$prefix); } + this.dispose(); + }, + + dispose() { + clearInterval(this.$$cacheFlushIntervalId); + clearInterval(this.$$recycleFreqId); $$storage = null; $$data = null; $$lruHeap = null; @@ -834,6 +838,13 @@ CacheFactory.destroy = cacheId => { } }; +CacheFactory.dispose = cacheId => { + if (caches[cacheId]) { + caches[cacheId].dispose(); + delete caches[cacheId]; + } +}; + CacheFactory.destroyAll = () => { for (var cacheId in caches) { caches[cacheId].destroy(); diff --git a/test/unit/Cache/index.dispose.test.js b/test/unit/Cache/index.dispose.test.js new file mode 100644 index 0000000..57e36ca --- /dev/null +++ b/test/unit/Cache/index.dispose.test.js @@ -0,0 +1,31 @@ +describe('Cache#dispose()', function () { + it('should remove the cache from the registry of caches.', function () { + var cache = TestCacheFactory('dispose-cache'); + cache.dispose(); + try { + assert.equal(cache.info(), { size: 0 }); + fail('should not be able to use a cache after disposing it'); + } catch (err) { + + } + assert.isUndefined(TestCacheFactory.get('dispose-cache')); + }); + it('should not remove items from localStorage when storageMode is used.', function () { + var localStorageCache = TestCacheFactory('dispose-lsc', { storageMode: 'localStorage', storagePrefix: 'acc.' }); + var sessionStorageCache = TestCacheFactory('dispose-ssc', { storageMode: 'sessionStorage' }); + + localStorageCache.put('item1', 'value1'); + sessionStorageCache.put('item1', 'value1'); + + var localStoragePrefix = localStorageCache.$$storagePrefix; + var sessionStoragePrefix = sessionStorageCache.$$storagePrefix; + + localStorageCache.dispose(); + sessionStorageCache.dispose(); + + assert.equal(JSON.parse(localStorage.getItem(localStoragePrefix + 'dispose-lsc.data.item1')).value, 'value1'); + assert.equal(localStorage.getItem(localStoragePrefix + 'dispose-lsc.keys'), '["item1"]'); + assert.equal(JSON.parse(sessionStorage.getItem(sessionStoragePrefix + 'dispose-ssc.data.item1')).value, 'value1'); + assert.equal(sessionStorage.getItem(sessionStoragePrefix + 'dispose-ssc.keys'), '["item1"]'); + }); +});