diff --git a/CHANGELOG.md b/CHANGELOG.md
index 146fc26..5688309 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,11 @@
+##### 4.2.0 27 April 2015
+
+###### Backwards compatible bug fixes
+- #174 - Cache not being fully emptied if using localStorage and multiple web pages
+
##### 4.1.0 30 March 2015
-###### Backwards compatible API fixes
+###### Backwards compatible API changes
- #169 - Official support for ngResource
##### 4.0.2 22 March 2015
diff --git a/dist/angular-cache.js b/dist/angular-cache.js
index 702a007..f2700bc 100644
--- a/dist/angular-cache.js
+++ b/dist/angular-cache.js
@@ -1,6 +1,6 @@
/*!
* angular-cache
- * @version 4.1.0 - Homepage
+ * @version 4.2.0 - Homepage
* @author Jason Dobry
* @copyright (c) 2013-2015 Jason Dobry
* @license MIT
@@ -93,7 +93,7 @@ return /******/ (function(modules) { // webpackBootstrap
var _keySet = function (collection) {
var keySet = {},
- key;
+ key = undefined;
for (key in collection) {
if (collection.hasOwnProperty(key)) {
keySet[key] = key;
@@ -201,7 +201,7 @@ return /******/ (function(modules) { // webpackBootstrap
};
this.peek = function () {
- return _this.heap[0];
+ return _this.heap.length ? _this.heap[0] : undefined;
};
this.pop = function () {
@@ -656,6 +656,7 @@ return /******/ (function(modules) { // webpackBootstrap
}
$$data = {};
}
+ $$promises = {};
},
removeExpired: function removeExpired() {
diff --git a/dist/angular-cache.min.js b/dist/angular-cache.min.js
index eac9ef6..8d86830 100644
--- a/dist/angular-cache.min.js
+++ b/dist/angular-cache.min.js
@@ -1,6 +1,6 @@
/*!
* angular-cache
-* @version 4.1.0 - Homepage
+* @version 4.2.0 - Homepage
* @author Jason Dobry
* @copyright (c) 2013-2015 Jason Dobry
* @license MIT
@@ -8,5 +8,5 @@
* @overview angular-cache is a very useful replacement for Angular's $cacheFactory.
*/
-!function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):"object"==typeof exports?exports.angularCacheModuleName=b(require("angular")):a.angularCacheModuleName=b(a.angular)}(this,function(a){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){function d(a,b,c){for(var d=a[c],e=b(d);c>0;){var f=Math.floor((c+1)/2)-1,g=a[f];if(e>=b(g))break;a[f]=d,a[c]=g,c=f}}var e=function(a){return a&&a.__esModule?a["default"]:a},f=function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")},g=e(c(1)),h=function(a){var b=[],c=void 0;for(c in a)a.hasOwnProperty(c)&&b.push(c);return b},i=function(a){return a&&"function"==typeof a.then},j=function(a){return g.isNumber(a)?a.toString():a},k=function(a){var b,c={};for(b in a)a.hasOwnProperty(b)&&(c[b]=b);return c},l=function(a,b,c){for(var d=a.length,e=a[c],f=b(e);;){var g=2*(c+1),h=g-1,i=null;if(d>h){var j=a[h],k=b(j);f>k&&(i=h)}if(d>g){var l=a[g],m=b(l);m<(null===i?f:b(a[h]))&&(i=g)}if(null===i)break;a[c]=a[i],a[i]=e,c=i}},m=function p(a,b){var c=this;if(f(this,p),a||(a=function(a){return a}),b||(b=function(a,b){return a===b}),"function"!=typeof a)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof b)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=a,this.compareFunc=b,this.heap=[],this.push=function(a){c.heap.push(a),d(c.heap,c.weightFunc,c.heap.length-1)},this.peek=function(){return c.heap[0]},this.pop=function(){var a=c.heap[0],b=c.heap.pop();return c.heap.length>0&&(c.heap[0]=b,l(c.heap,c.weightFunc,0)),a},this.remove=function(a){for(var b=c.heap.length,e=0;b>e;e++)if(c.compareFunc(c.heap[e],a)){var f=c.heap[e],g=c.heap.pop();return e!==b-1&&(c.heap[e]=g,d(c.heap,c.weightFunc,e),l(c.heap,c.weightFunc,e)),f}return null},this.removeAll=function(){c.heap=[]},this.size=function(){return c.heap.length}},n=function q(){f(this,q),this.$get=function(){return m}},o=function r(){var a=this;f(this,r);var b=this.defaults={capacity:Number.MAX_VALUE,maxAge:Number.MAX_VALUE,deleteOnExpire:"none",onExpire:null,cacheFlushInterval:null,recycleFreq:1e3,storageMode:"memory",storageImpl:null,disabled:!1,storagePrefix:"angular-cache.caches.",storeOnResolve:!1,storeOnReject:!1};this.$get=["$q",function(c){function d(a,b){return f(a,b)}var e={},f=function(a,d){if(a in e)throw new Error(""+a+" already exists!");if(!g.isString(a))throw new Error("cacheId must be a string!");var f={},l={},n=null,o=new m(function(a){return a.expires},g.equals),p=new m(function(a){return a.accessed},g.equals),q=e[a]={$$id:a,destroy:function(){clearInterval(this.$$cacheFlushIntervalId),clearInterval(this.$$recycleFreqId),this.removeAll(),n&&(n().removeItem(""+this.$$prefix+".keys"),n().removeItem(this.$$prefix)),n=null,f=null,p=null,o=null,this.$$prefix=null,delete e[this.$$id]},disable:function(){this.$$disabled=!0},enable:function(){delete this.$$disabled},get:function(a,b){var c=this;if(g.isArray(a)){var d=function(){var d=a,e=[];return g.forEach(d,function(a){var d=c.get(a,b);null!==d&&void 0!==d&&e.push(d)}),{v:e}}();if("object"==typeof d)return d.v}else if(a=j(a),this.$$disabled)return;if(b=b||{},!g.isString(a))throw new Error("key must be a string!");if(b&&!g.isObject(b))throw new Error("options must be an object!");if(b.onExpire&&!g.isFunction(b.onExpire))throw new Error("options.onExpire must be a function!");var e=void 0;if(n){if(l[a])return l[a];var h=n().getItem(""+this.$$prefix+".data."+a);if(!h)return;e=g.fromJson(h)}else{if(!(a in f))return;e=f[a]}var i=e.value,k=(new Date).getTime();return n?(p.remove({key:a,accessed:e.accessed}),e.accessed=k,p.push({key:a,accessed:k})):(p.remove(e),e.accessed=k,p.push(e)),"passive"===this.$$deleteOnExpire&&"expires"in e&&e.expiresthis.$$maxAge}):void 0}return a in f?(b=f[a],{created:b.created,accessed:b.accessed,expires:b.expires,isExpired:(new Date).getTime()-b.created>this.$$maxAge}):void 0}return{id:this.$$id,capacity:this.$$capacity,maxAge:this.$$maxAge,deleteOnExpire:this.$$deleteOnExpire,onExpire:this.$$onExpire,cacheFlushInterval:this.$$cacheFlushInterval,recycleFreq:this.$$recycleFreq,storageMode:this.$$storageMode,storageImpl:n?n():void 0,disabled:!!this.$$disabled,size:p&&p.size()||0}},keys:function(){if(n){var a=n().getItem(""+this.$$prefix+".keys");return a?g.fromJson(a):[]}return h(f)},keySet:function(){if(n){var a=n().getItem(""+this.$$prefix+".keys"),b={};if(a)for(var c=g.fromJson(a),d=0;dthis.$$capacity&&this.remove(p.peek().key),b}},remove:function(a){if(a+="",delete l[a],!n){var b=f[a]?f[a].value:void 0;return p.remove(f[a]),o.remove(f[a]),f[a]=null,delete f[a],b}var c=n().getItem(""+this.$$prefix+".data."+a);if(c){var d=g.fromJson(c);p.remove({key:a,accessed:d.accessed}),o.remove({key:a,expires:d.expires}),n().removeItem(""+this.$$prefix+".data."+a);var e=n().getItem(""+this.$$prefix+".keys"),h=e?g.fromJson(e):[],i=h.indexOf(a);return i>=0&&h.splice(i,1),n().setItem(""+this.$$prefix+".keys",JSON.stringify(h)),d.value}},removeAll:function(){if(n){p.removeAll(),o.removeAll();var a=n().getItem(""+this.$$prefix+".keys");if(a)for(var b=g.fromJson(a),c=0;ca)throw new Error("cacheFlushInterval must be greater than zero!");a!==this.$$cacheFlushInterval&&(this.$$cacheFlushInterval=a,clearInterval(this.$$cacheFlushIntervalId),function(a){a.$$cacheFlushIntervalId=setInterval(function(){a.removeAll()},a.$$cacheFlushInterval)}(this))}},setCapacity:function(a){if(null===a)delete this.$$capacity;else{if(!g.isNumber(a))throw new Error("capacity must be a number!");if(0>a)throw new Error("capacity must be greater than zero!");this.$$capacity=a}for(var b={};p.size()>this.$$capacity;)b[p.peek().key]=this.remove(p.peek().key);return b},setDeleteOnExpire:function(a,b){if(null===a)delete this.$$deleteOnExpire;else{if(!g.isString(a))throw new Error("deleteOnExpire must be a string!");if("none"!==a&&"passive"!==a&&"aggressive"!==a)throw new Error('deleteOnExpire must be "none", "passive" or "aggressive"!');this.$$deleteOnExpire=a}b!==!1&&this.setRecycleFreq(this.$$recycleFreq)},setMaxAge:function(a){if(null===a)this.$$maxAge=Number.MAX_VALUE;else{if(!g.isNumber(a))throw new Error("maxAge must be a number!");if(0>a)throw new Error("maxAge must be greater than zero!");this.$$maxAge=a}var b=void 0,c=void 0,d=void 0;if(o.removeAll(),n){var e=n().getItem(""+this.$$prefix+".keys");for(c=e?g.fromJson(e):[],b=0;ba)throw new Error("recycleFreq must be greater than zero!");this.$$recycleFreq=a}clearInterval(this.$$recycleFreqId),"aggressive"===this.$$deleteOnExpire?!function(a){a.$$recycleFreqId=setInterval(function(){a.removeExpired()},a.$$recycleFreq)}(this):delete this.$$recycleFreqId},setStorageMode:function(a,b){if(!g.isString(a))throw new Error("storageMode must be a string!");if("memory"!==a&&"localStorage"!==a&&"sessionStorage"!==a)throw new Error('storageMode must be "memory", "localStorage" or "sessionStorage"!');var c=!1,d={};if("string"==typeof this.$$storageMode&&this.$$storageMode!==a){var e=this.keys();if(e.length){for(var f=0;f0;){var f=Math.floor((c+1)/2)-1,g=a[f];if(e>=b(g))break;a[f]=d,a[c]=g,c=f}}var e=function(a){return a&&a.__esModule?a["default"]:a},f=function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")},g=e(c(1)),h=function(a){var b=[],c=void 0;for(c in a)a.hasOwnProperty(c)&&b.push(c);return b},i=function(a){return a&&"function"==typeof a.then},j=function(a){return g.isNumber(a)?a.toString():a},k=function(a){var b={},c=void 0;for(c in a)a.hasOwnProperty(c)&&(b[c]=c);return b},l=function(a,b,c){for(var d=a.length,e=a[c],f=b(e);;){var g=2*(c+1),h=g-1,i=null;if(d>h){var j=a[h],k=b(j);f>k&&(i=h)}if(d>g){var l=a[g],m=b(l);m<(null===i?f:b(a[h]))&&(i=g)}if(null===i)break;a[c]=a[i],a[i]=e,c=i}},m=function p(a,b){var c=this;if(f(this,p),a||(a=function(a){return a}),b||(b=function(a,b){return a===b}),"function"!=typeof a)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof b)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=a,this.compareFunc=b,this.heap=[],this.push=function(a){c.heap.push(a),d(c.heap,c.weightFunc,c.heap.length-1)},this.peek=function(){return c.heap.length?c.heap[0]:void 0},this.pop=function(){var a=c.heap[0],b=c.heap.pop();return c.heap.length>0&&(c.heap[0]=b,l(c.heap,c.weightFunc,0)),a},this.remove=function(a){for(var b=c.heap.length,e=0;b>e;e++)if(c.compareFunc(c.heap[e],a)){var f=c.heap[e],g=c.heap.pop();return e!==b-1&&(c.heap[e]=g,d(c.heap,c.weightFunc,e),l(c.heap,c.weightFunc,e)),f}return null},this.removeAll=function(){c.heap=[]},this.size=function(){return c.heap.length}},n=function q(){f(this,q),this.$get=function(){return m}},o=function r(){var a=this;f(this,r);var b=this.defaults={capacity:Number.MAX_VALUE,maxAge:Number.MAX_VALUE,deleteOnExpire:"none",onExpire:null,cacheFlushInterval:null,recycleFreq:1e3,storageMode:"memory",storageImpl:null,disabled:!1,storagePrefix:"angular-cache.caches.",storeOnResolve:!1,storeOnReject:!1};this.$get=["$q",function(c){function d(a,b){return f(a,b)}var e={},f=function(a,d){if(a in e)throw new Error(""+a+" already exists!");if(!g.isString(a))throw new Error("cacheId must be a string!");var f={},l={},n=null,o=new m(function(a){return a.expires},g.equals),p=new m(function(a){return a.accessed},g.equals),q=e[a]={$$id:a,destroy:function(){clearInterval(this.$$cacheFlushIntervalId),clearInterval(this.$$recycleFreqId),this.removeAll(),n&&(n().removeItem(""+this.$$prefix+".keys"),n().removeItem(this.$$prefix)),n=null,f=null,p=null,o=null,this.$$prefix=null,delete e[this.$$id]},disable:function(){this.$$disabled=!0},enable:function(){delete this.$$disabled},get:function(a,b){var c=this;if(g.isArray(a)){var d=function(){var d=a,e=[];return g.forEach(d,function(a){var d=c.get(a,b);null!==d&&void 0!==d&&e.push(d)}),{v:e}}();if("object"==typeof d)return d.v}else if(a=j(a),this.$$disabled)return;if(b=b||{},!g.isString(a))throw new Error("key must be a string!");if(b&&!g.isObject(b))throw new Error("options must be an object!");if(b.onExpire&&!g.isFunction(b.onExpire))throw new Error("options.onExpire must be a function!");var e=void 0;if(n){if(l[a])return l[a];var h=n().getItem(""+this.$$prefix+".data."+a);if(!h)return;e=g.fromJson(h)}else{if(!(a in f))return;e=f[a]}var i=e.value,k=(new Date).getTime();return n?(p.remove({key:a,accessed:e.accessed}),e.accessed=k,p.push({key:a,accessed:k})):(p.remove(e),e.accessed=k,p.push(e)),"passive"===this.$$deleteOnExpire&&"expires"in e&&e.expiresthis.$$maxAge}):void 0}return a in f?(b=f[a],{created:b.created,accessed:b.accessed,expires:b.expires,isExpired:(new Date).getTime()-b.created>this.$$maxAge}):void 0}return{id:this.$$id,capacity:this.$$capacity,maxAge:this.$$maxAge,deleteOnExpire:this.$$deleteOnExpire,onExpire:this.$$onExpire,cacheFlushInterval:this.$$cacheFlushInterval,recycleFreq:this.$$recycleFreq,storageMode:this.$$storageMode,storageImpl:n?n():void 0,disabled:!!this.$$disabled,size:p&&p.size()||0}},keys:function(){if(n){var a=n().getItem(""+this.$$prefix+".keys");return a?g.fromJson(a):[]}return h(f)},keySet:function(){if(n){var a=n().getItem(""+this.$$prefix+".keys"),b={};if(a)for(var c=g.fromJson(a),d=0;dthis.$$capacity&&this.remove(p.peek().key),b}},remove:function(a){if(a+="",delete l[a],!n){var b=f[a]?f[a].value:void 0;return p.remove(f[a]),o.remove(f[a]),f[a]=null,delete f[a],b}var c=n().getItem(""+this.$$prefix+".data."+a);if(c){var d=g.fromJson(c);p.remove({key:a,accessed:d.accessed}),o.remove({key:a,expires:d.expires}),n().removeItem(""+this.$$prefix+".data."+a);var e=n().getItem(""+this.$$prefix+".keys"),h=e?g.fromJson(e):[],i=h.indexOf(a);return i>=0&&h.splice(i,1),n().setItem(""+this.$$prefix+".keys",JSON.stringify(h)),d.value}},removeAll:function(){if(n){p.removeAll(),o.removeAll();var a=n().getItem(""+this.$$prefix+".keys");if(a)for(var b=g.fromJson(a),c=0;ca)throw new Error("cacheFlushInterval must be greater than zero!");a!==this.$$cacheFlushInterval&&(this.$$cacheFlushInterval=a,clearInterval(this.$$cacheFlushIntervalId),function(a){a.$$cacheFlushIntervalId=setInterval(function(){a.removeAll()},a.$$cacheFlushInterval)}(this))}},setCapacity:function(a){if(null===a)delete this.$$capacity;else{if(!g.isNumber(a))throw new Error("capacity must be a number!");if(0>a)throw new Error("capacity must be greater than zero!");this.$$capacity=a}for(var b={};p.size()>this.$$capacity;)b[p.peek().key]=this.remove(p.peek().key);return b},setDeleteOnExpire:function(a,b){if(null===a)delete this.$$deleteOnExpire;else{if(!g.isString(a))throw new Error("deleteOnExpire must be a string!");if("none"!==a&&"passive"!==a&&"aggressive"!==a)throw new Error('deleteOnExpire must be "none", "passive" or "aggressive"!');this.$$deleteOnExpire=a}b!==!1&&this.setRecycleFreq(this.$$recycleFreq)},setMaxAge:function(a){if(null===a)this.$$maxAge=Number.MAX_VALUE;else{if(!g.isNumber(a))throw new Error("maxAge must be a number!");if(0>a)throw new Error("maxAge must be greater than zero!");this.$$maxAge=a}var b=void 0,c=void 0,d=void 0;if(o.removeAll(),n){var e=n().getItem(""+this.$$prefix+".keys");for(c=e?g.fromJson(e):[],b=0;ba)throw new Error("recycleFreq must be greater than zero!");this.$$recycleFreq=a}clearInterval(this.$$recycleFreqId),"aggressive"===this.$$deleteOnExpire?!function(a){a.$$recycleFreqId=setInterval(function(){a.removeExpired()},a.$$recycleFreq)}(this):delete this.$$recycleFreqId},setStorageMode:function(a,b){if(!g.isString(a))throw new Error("storageMode must be a string!");if("memory"!==a&&"localStorage"!==a&&"sessionStorage"!==a)throw new Error('storageMode must be "memory", "localStorage" or "sessionStorage"!');var c=!1,d={};if("string"==typeof this.$$storageMode&&this.$$storageMode!==a){var e=this.keys();if(e.length){for(var f=0;f {
};
let _keySet = collection => {
- var keySet = {}, key;
+ let keySet = {}, key;
for (key in collection) {
if (collection.hasOwnProperty(key)) {
keySet[key] = key;
@@ -122,9 +122,7 @@ class BinaryHeap {
bubbleUp(this.heap, this.weightFunc, this.heap.length - 1);
};
- this.peek = () => {
- return this.heap[0];
- };
+ this.peek = () => this.heap.length ? this.heap[0] : undefined;
this.pop = () => {
let front = this.heap[0];
@@ -137,8 +135,8 @@ class BinaryHeap {
};
this.remove = node => {
- var length = this.heap.length;
- for (let i = 0; i < length; i++) {
+ let length = this.heap.length;
+ for (var i = 0; i < length; i++) {
if (this.compareFunc(this.heap[i], node)) {
let removed = this.heap[i];
let end = this.heap.pop();
@@ -157,9 +155,7 @@ class BinaryHeap {
this.heap = [];
};
- this.size = () => {
- return this.heap.length;
- };
+ this.size = () => this.heap.length;
}
}
@@ -171,7 +167,7 @@ class BinaryHeapProvider {
class CacheFactoryProvider {
constructor() {
- var defaults = this.defaults = {
+ let defaults = this.defaults = {
capacity: Number.MAX_VALUE,
maxAge: Number.MAX_VALUE,
deleteOnExpire: 'none',
@@ -321,7 +317,7 @@ class CacheFactoryProvider {
if (key) {
let item;
if ($$storage) {
- var itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
+ let itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
if (itemJson) {
item = angular.fromJson(itemJson);
@@ -385,7 +381,7 @@ class CacheFactoryProvider {
let kSet = {};
if (keysJson) {
- var keys = angular.fromJson(keysJson);
+ let keys = angular.fromJson(keysJson);
for (var i = 0; i < keys.length; i++) {
kSet[keys[i]] = keys[i];
@@ -431,8 +427,8 @@ class CacheFactoryProvider {
throw new Error('key must be a string!');
}
- var now = new Date().getTime();
- var item = {
+ let now = new Date().getTime();
+ let item = {
key: key,
value: _isPromiseLike(value) ? value.then(getHandler(storeOnResolve, false), getHandler(storeOnReject, true)) : value,
created: now,
@@ -446,9 +442,9 @@ class CacheFactoryProvider {
$$promises[key] = item.value;
return $$promises[key];
}
- var keysJson = $$storage().getItem(`${this.$$prefix}.keys`);
- var keys = keysJson ? angular.fromJson(keysJson) : [];
- var itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
+ let keysJson = $$storage().getItem(`${this.$$prefix}.keys`);
+ let keys = keysJson ? angular.fromJson(keysJson) : [];
+ let itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
// Remove existing
if (itemJson) {
@@ -466,7 +462,7 @@ class CacheFactoryProvider {
});
// Set item
$$storage().setItem(`${this.$$prefix}.data.${key}`, JSON.stringify(item));
- var exists = false;
+ let exists = false;
for (var i = 0; i < keys.length; i++) {
if (keys[i] === key) {
exists = true;
@@ -527,7 +523,7 @@ class CacheFactoryProvider {
return item.value;
}
} else {
- var value = $$data[key] ? $$data[key].value : undefined;
+ let value = $$data[key] ? $$data[key].value : undefined;
$$lruHeap.remove($$data[key]);
$$expiresHeap.remove($$data[key]);
$$data[key] = null;
@@ -558,6 +554,7 @@ class CacheFactoryProvider {
}
$$data = {};
}
+ $$promises = {};
},
removeExpired() {
@@ -573,7 +570,7 @@ class CacheFactoryProvider {
if ($$storage) {
for (key in expired) {
- var itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
+ let itemJson = $$storage().getItem(`${this.$$prefix}.data.${key}`);
if (itemJson) {
expired[key] = angular.fromJson(itemJson).value;
this.remove(key);
@@ -622,7 +619,7 @@ class CacheFactoryProvider {
} else {
this.$$capacity = capacity;
}
- var removed = {};
+ let removed = {};
while ($$lruHeap.size() > this.$$capacity) {
removed[$$lruHeap.peek().key] = this.remove($$lruHeap.peek().key);
}