diff --git a/dist/angular-storage.js b/dist/angular-storage.js index 070312f06f19740401659ae530aeffdff5f2aa9b..9abe2172948d0e1d12cdaec64afb6d883010c182 100755 --- a/dist/angular-storage.js +++ b/dist/angular-storage.js @@ -30,8 +30,12 @@ angular.module('angular-storage.cookieStorage', []) angular.module('angular-storage.internalStore', ['angular-storage.localStorage', 'angular-storage.sessionStorage']) .factory('InternalStore', ["$log", "$injector", function($log, $injector) { - function InternalStore(namespace, storage, delimiter) { + function InternalStore(namespace, storage, delimiter, useCache) { this.namespace = namespace || null; + if (angular.isUndefined(useCache) || useCache == null) { + useCache = true; + } + this.useCache = useCache; this.delimiter = delimiter || '.'; this.inMemoryCache = {}; this.storage = $injector.get(storage || 'localStorage'); @@ -46,13 +50,15 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', }; InternalStore.prototype.set = function(name, elem) { - this.inMemoryCache[name] = elem; + if (this.useCache) { + this.inMemoryCache[name] = elem; + } this.storage.set(this.getNamespacedKey(name), JSON.stringify(elem)); }; InternalStore.prototype.get = function(name) { var obj = null; - if (name in this.inMemoryCache) { + if (this.useCache && name in this.inMemoryCache) { return this.inMemoryCache[name]; } var saved = this.storage.get(this.getNamespacedKey(name)); @@ -64,7 +70,9 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', obj = JSON.parse(saved); } - this.inMemoryCache[name] = obj; + if (this.useCache) { + this.inMemoryCache[name] = obj; + } } catch(e) { $log.error('Error parsing saved value', e); this.remove(name); @@ -73,7 +81,9 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', }; InternalStore.prototype.remove = function(name) { - this.inMemoryCache[name] = null; + if (this.useCache) { + this.inMemoryCache[name] = null; + } this.storage.remove(this.getNamespacedKey(name)); }; @@ -153,6 +163,9 @@ angular.module('angular-storage.store', ['angular-storage.internalStore']) // the default storage var _storage = 'localStorage'; + //caching is on by default + var _caching = true; + /** * Sets the storage. * @@ -164,19 +177,29 @@ angular.module('angular-storage.store', ['angular-storage.internalStore']) } }; + /** + * Sets the internal cache usage + * + * @param {boolean} useCache Whether to use internal cache + */ + this.setCaching = function(useCache) { + _caching = !!useCache; + }; + this.$get = ["InternalStore", function(InternalStore) { - var store = new InternalStore(null, _storage); + var store = new InternalStore(null, _storage, null, _caching); /** * Returns a namespaced store * * @param {String} namespace The namespace * @param {String} storage The name of the storage service - * @param {String} key The key + * @param {String} delimiter The key delimiter + * @param {boolean} useCache whether to use the internal caching * @returns {InternalStore} */ - store.getNamespacedStore = function(namespace, storage, key) { - return new InternalStore(namespace, storage, key); + store.getNamespacedStore = function(namespace, storage, delimiter, useCache) { + return new InternalStore(namespace, storage, delimiter, useCache); }; return store; diff --git a/dist/angular-storage.min.js b/dist/angular-storage.min.js index c30281e987c9f5b0eea61b1cb0a76d878171ab0d..acd0b1fc98de1825fbc9f6e4d1f72daf27ed9090 100755 --- a/dist/angular-storage.min.js +++ b/dist/angular-storage.min.js @@ -1 +1 @@ -!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.cookieStorage",[]).service("cookieStorage",["$injector",function(e){var t=e.get("$cookieStore");this.set=function(e,r){return t.put(e,r)},this.get=function(e){return t.get(e)},this.remove=function(e){return t.remove(e)}}]),angular.module("angular-storage.internalStore",["angular-storage.localStorage","angular-storage.sessionStorage"]).factory("InternalStore",["$log","$injector",function(e,t){function r(e,r,o){this.namespace=e||null,this.delimiter=o||".",this.inMemoryCache={},this.storage=t.get(r||"localStorage")}return r.prototype.getNamespacedKey=function(e){return this.namespace?[this.namespace,e].join(this.delimiter):e},r.prototype.set=function(e,t){this.inMemoryCache[e]=t,this.storage.set(this.getNamespacedKey(e),JSON.stringify(t))},r.prototype.get=function(t){var r=null;if(t in this.inMemoryCache)return this.inMemoryCache[t];var o=this.storage.get(this.getNamespacedKey(t));try{r="undefined"==typeof o||"undefined"===o?void 0:JSON.parse(o),this.inMemoryCache[t]=r}catch(a){e.error("Error parsing saved value",a),this.remove(t)}return r},r.prototype.remove=function(e){this.inMemoryCache[e]=null,this.storage.remove(this.getNamespacedKey(e))},r}]),angular.module("angular-storage.localStorage",["angular-storage.cookieStorage"]).service("localStorage",["$window","$injector",function(e,t){var r;try{e.localStorage.setItem("testKey","test"),e.localStorage.removeItem("testKey"),r=!0}catch(o){r=!1}if(r)this.set=function(t,r){return e.localStorage.setItem(t,r)},this.get=function(t){return e.localStorage.getItem(t)},this.remove=function(t){return e.localStorage.removeItem(t)};else{var a=t.get("cookieStorage");this.set=a.set,this.get=a.get,this.remove=a.remove}}]),angular.module("angular-storage.sessionStorage",["angular-storage.cookieStorage"]).service("sessionStorage",["$window","$injector",function(e,t){var r;try{e.sessionStorage.setItem("testKey","test"),e.sessionStorage.removeItem("testKey"),r=!0}catch(o){r=!1}if(r)this.set=function(t,r){return e.sessionStorage.setItem(t,r)},this.get=function(t){return e.sessionStorage.getItem(t)},this.remove=function(t){return e.sessionStorage.removeItem(t)};else{var a=t.get("cookieStorage");this.set=a.set,this.get=a.get,this.remove=a.remove}}]),angular.module("angular-storage.store",["angular-storage.internalStore"]).provider("store",function(){var e="localStorage";this.setStore=function(t){t&&angular.isString(t)&&(e=t)},this.$get=["InternalStore",function(t){var r=new t(null,e);return r.getNamespacedStore=function(e,r,o){return new t(e,r,o)},r}]})}(); \ No newline at end of file +!function(){angular.module("angular-storage",["angular-storage.store"]),angular.module("angular-storage.cookieStorage",[]).service("cookieStorage",["$injector",function(e){var t=e.get("$cookieStore");this.set=function(e,r){return t.put(e,r)},this.get=function(e){return t.get(e)},this.remove=function(e){return t.remove(e)}}]),angular.module("angular-storage.internalStore",["angular-storage.localStorage","angular-storage.sessionStorage"]).factory("InternalStore",["$log","$injector",function(e,t){function r(e,r,o,a){this.namespace=e||null,(angular.isUndefined(a)||null==a)&&(a=!0),this.useCache=a,this.delimiter=o||".",this.inMemoryCache={},this.storage=t.get(r||"localStorage")}return r.prototype.getNamespacedKey=function(e){return this.namespace?[this.namespace,e].join(this.delimiter):e},r.prototype.set=function(e,t){this.useCache&&(this.inMemoryCache[e]=t),this.storage.set(this.getNamespacedKey(e),JSON.stringify(t))},r.prototype.get=function(t){var r=null;if(this.useCache&&t in this.inMemoryCache)return this.inMemoryCache[t];var o=this.storage.get(this.getNamespacedKey(t));try{r="undefined"==typeof o||"undefined"===o?void 0:JSON.parse(o),this.useCache&&(this.inMemoryCache[t]=r)}catch(a){e.error("Error parsing saved value",a),this.remove(t)}return r},r.prototype.remove=function(e){this.useCache&&(this.inMemoryCache[e]=null),this.storage.remove(this.getNamespacedKey(e))},r}]),angular.module("angular-storage.localStorage",["angular-storage.cookieStorage"]).service("localStorage",["$window","$injector",function(e,t){var r;try{e.localStorage.setItem("testKey","test"),e.localStorage.removeItem("testKey"),r=!0}catch(o){r=!1}if(r)this.set=function(t,r){return e.localStorage.setItem(t,r)},this.get=function(t){return e.localStorage.getItem(t)},this.remove=function(t){return e.localStorage.removeItem(t)};else{var a=t.get("cookieStorage");this.set=a.set,this.get=a.get,this.remove=a.remove}}]),angular.module("angular-storage.sessionStorage",["angular-storage.cookieStorage"]).service("sessionStorage",["$window","$injector",function(e,t){var r;try{e.sessionStorage.setItem("testKey","test"),e.sessionStorage.removeItem("testKey"),r=!0}catch(o){r=!1}if(r)this.set=function(t,r){return e.sessionStorage.setItem(t,r)},this.get=function(t){return e.sessionStorage.getItem(t)},this.remove=function(t){return e.sessionStorage.removeItem(t)};else{var a=t.get("cookieStorage");this.set=a.set,this.get=a.get,this.remove=a.remove}}]),angular.module("angular-storage.store",["angular-storage.internalStore"]).provider("store",function(){var e="localStorage",t=!0;this.setStore=function(t){t&&angular.isString(t)&&(e=t)},this.setCaching=function(e){t=!!e},this.$get=["InternalStore",function(r){var o=new r(null,e,null,t);return o.getNamespacedStore=function(e,t,o,a){return new r(e,t,o,a)},o}]})}(); \ No newline at end of file diff --git a/src/angularStorage/services/internalStore.js b/src/angularStorage/services/internalStore.js index df3157fbd08c70888f6800981163d28b8e8adfa2..b3fc930ae69128f8a18dddd58bc7fc2997d085f4 100644 --- a/src/angularStorage/services/internalStore.js +++ b/src/angularStorage/services/internalStore.js @@ -1,8 +1,12 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', 'angular-storage.sessionStorage']) .factory('InternalStore', function($log, $injector) { - function InternalStore(namespace, storage, delimiter) { + function InternalStore(namespace, storage, delimiter, useCache) { this.namespace = namespace || null; + if (angular.isUndefined(useCache) || useCache == null) { + useCache = true; + } + this.useCache = useCache; this.delimiter = delimiter || '.'; this.inMemoryCache = {}; this.storage = $injector.get(storage || 'localStorage'); @@ -17,13 +21,15 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', }; InternalStore.prototype.set = function(name, elem) { - this.inMemoryCache[name] = elem; + if (this.useCache) { + this.inMemoryCache[name] = elem; + } this.storage.set(this.getNamespacedKey(name), JSON.stringify(elem)); }; InternalStore.prototype.get = function(name) { var obj = null; - if (name in this.inMemoryCache) { + if (this.useCache && name in this.inMemoryCache) { return this.inMemoryCache[name]; } var saved = this.storage.get(this.getNamespacedKey(name)); @@ -35,7 +41,9 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', obj = JSON.parse(saved); } - this.inMemoryCache[name] = obj; + if (this.useCache) { + this.inMemoryCache[name] = obj; + } } catch(e) { $log.error('Error parsing saved value', e); this.remove(name); @@ -44,7 +52,9 @@ angular.module('angular-storage.internalStore', ['angular-storage.localStorage', }; InternalStore.prototype.remove = function(name) { - this.inMemoryCache[name] = null; + if (this.useCache) { + this.inMemoryCache[name] = null; + } this.storage.remove(this.getNamespacedKey(name)); }; diff --git a/src/angularStorage/services/store.js.js b/src/angularStorage/services/store.js.js index d84ad2da7a21dfa7d66ec7ac377953ccd6bf94a9..18773c60155ff4da624f042e11de2ad7c740d559 100644 --- a/src/angularStorage/services/store.js.js +++ b/src/angularStorage/services/store.js.js @@ -4,6 +4,9 @@ angular.module('angular-storage.store', ['angular-storage.internalStore']) // the default storage var _storage = 'localStorage'; + //caching is on by default + var _caching = true; + /** * Sets the storage. * @@ -15,19 +18,29 @@ angular.module('angular-storage.store', ['angular-storage.internalStore']) } }; + /** + * Sets the internal cache usage + * + * @param {boolean} useCache Whether to use internal cache + */ + this.setCaching = function(useCache) { + _caching = !!useCache; + }; + this.$get = function(InternalStore) { - var store = new InternalStore(null, _storage); + var store = new InternalStore(null, _storage, null, _caching); /** * Returns a namespaced store * * @param {String} namespace The namespace * @param {String} storage The name of the storage service - * @param {String} key The key + * @param {String} delimiter The key delimiter + * @param {boolean} useCache whether to use the internal caching * @returns {InternalStore} */ - store.getNamespacedStore = function(namespace, storage, key) { - return new InternalStore(namespace, storage, key); + store.getNamespacedStore = function(namespace, storage, delimiter, useCache) { + return new InternalStore(namespace, storage, delimiter, useCache); }; return store; diff --git a/test/unit/angularStorage/services/storeSpec.js b/test/unit/angularStorage/services/storeSpec.js index fe1e4e6bc3d38bf3a988fafa53e3c3bca2dfb29a..44f7fd31468ce5875b9fb463260480053ac599b1 100644 --- a/test/unit/angularStorage/services/storeSpec.js +++ b/test/unit/angularStorage/services/storeSpec.js @@ -69,6 +69,54 @@ describe('angularStorage store', function() { })); }); +describe('angularStorage storeProvider.setCaching(false)', function () { + var provider; + + beforeEach(function() { + module('angular-storage.store', function(storeProvider) { + provider = storeProvider; + provider.setCaching(false); + }); + }); + + it('should not store into internal cache', inject(function(store) { + var value1 = 'some value'; + var value2 = 256; + store.set('key1', value1); + store.set('key2', value2); + store.remove('key1'); + + expect(store.inMemoryCache).to.be.empty; + expect(store.get('key2')).to.equal(value2); + })); + + it('should store into internal cache in namespaced store when default caching', inject(function(store) { + var namespacedStore = store.getNamespacedStore('bb'); + var value1 = 'some value'; + var value2 = 256; + + namespacedStore.set('key1', value1); + namespacedStore.set('key2', value2); + + expect(namespacedStore.inMemoryCache).not.to.be.empty; + expect(namespacedStore.inMemoryCache).to.have.property('key1'); + expect(namespacedStore.inMemoryCache).to.have.property('key2'); + })); + + it('should not store into internal cache in namespaced store when caching=false', inject(function(store) { + var namespacedStore = store.getNamespacedStore('bb', 'localStorage', null, false); + var value1 = 'some value'; + var value2 = 256; + + namespacedStore.set('key1', value1); + namespacedStore.set('key2', value2); + + expect(namespacedStore.inMemoryCache).to.be.empty; + expect(namespacedStore.get('key1')).to.equal(value1); + expect(namespacedStore.get('key2')).to.equal(value2); + })); +}); + describe('angularStorage storeProvider.setStore("sessionStorage")', function () { var provider; @@ -385,7 +433,7 @@ describe('angularStorage new namespaced store', function() { it('should should save items correctly when the delimiter is set', inject(function(store, $window) { var value = 111; - var aStore = store.getNamespacedStore('aa', 'sessionStorage', '-'); + var aStore = store.getNamespacedStore('aa', 'sessionStorage', '-', true); aStore.set('wayne', value); expect(aStore.get('wayne')).to.equal(value);