Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions test/unit/Cache/index.dispose.test.js
Original file line number Diff line number Diff line change
@@ -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"]');
});
});