Skip to content
This repository was archived by the owner on Sep 15, 2022. It is now read-only.
Merged
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
41 changes: 32 additions & 9 deletions dist/angular-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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));
};

Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions src/angularStorage/services/internalStore.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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));
};

Expand Down
21 changes: 17 additions & 4 deletions src/angularStorage/services/store.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
50 changes: 49 additions & 1 deletion test/unit/angularStorage/services/storeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down