/**
* @author Jason Dobry <jason.dobry@gmail.com>
- * @file angular-cache-1.1.0.js
- * @version 1.1.0 - [Homepage]{@link http://jmdobry.github.io/angular-cache/}
+ * @file angular-cache-1.2.0.js
+ * @version 1.2.0 - [Homepage]{@link http://jmdobry.github.io/angular-cache/}
* @copyright (c) 2013 Jason Dobry <http://jmdobry.github.io/angular-cache>
* @license MIT <https://github.com/jmdobry/angular-cache/blob/master/LICENSE>
*
@@ -325,7 +325,7 @@ Source: angular-cache-1.1.0.js
if (!cacheDirty) {
_loadCacheConfig();
} else {
- _saveCacheConfig();
+ _syncToStorage(null);
keys = _keys(data);
for (i = 0; i < keys.length; i++) {
storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]]));
@@ -340,7 +340,7 @@ Source: angular-cache-1.1.0.js
if (!cacheDirty) {
_loadCacheConfig();
} else {
- _saveCacheConfig();
+ _syncToStorage(null);
keys = _keys(data);
for (i = 0; i < keys.length; i++) {
storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]]));
@@ -497,22 +497,26 @@ Source: angular-cache-1.1.0.js
if (data.hasOwnProperty('aggressiveDelete')) {
options.aggressiveDelete = data.aggressiveDelete;
}
- self.put(keys[i], data.value);
+ self.put(keys[i], data.value, options);
}
}
- _saveCacheConfig();
+ _syncToStorage(null, null);
}
}
/**
- * @method _saveCacheConfig
- * @desc If storageMode is set, save current keys of cache to localStorage.
+ * @method _syncToStorage
+ * @desc If storageMode is set, sync to localStorage.
+ * @param {String} key The identifier of the item to sync.
* @private
* @ignore
*/
- function _saveCacheConfig() {
+ function _syncToStorage(key) {
if (config.storageMode && storage) {
storage.setItem(prefix + '.keys', angular.toJson(_keys(data)));
+ if (key) {
+ storage.setItem(prefix + '.data.' + key, angular.toJson(data[key]));
+ }
}
}
@@ -521,7 +525,7 @@ Source: angular-cache-1.1.0.js
* @desc Add a key-value pair with timestamp to the cache.
* @param {String} key The identifier for the item to add to the cache.
* @param {*} value The value of the item to add to the cache.
- * @param {Object} [options] { maxAge: {Number} }
+ * @param {Object} [options] {{ maxAge: {Number}, aggressiveDelete: {Boolean}, timestamp: {Number} }}
* @returns {*} value The value of the item added to the cache.
* @privileged
*/
@@ -552,12 +556,20 @@ Source: angular-cache-1.1.0.js
if (!(key in data)) {
size++;
+ } else {
+ if (data[key].timeoutId) {
+ $timeout.cancel(data[key].timeoutId);
+ }
}
data[key] = {
value: value
};
+ if (options && options.maxAge) {
+ data[key].maxAge = options.maxAge;
+ }
+
if (options && options.hasOwnProperty('aggressiveDelete')) {
data[key].aggressiveDelete = options.aggressiveDelete;
}
@@ -565,19 +577,12 @@ Source: angular-cache-1.1.0.js
data[key].timestamp = (options && options.timestamp) || new Date().getTime();
if ((options && options.maxAge) || config.maxAge) {
- if (data[key].timeoutId) {
- $timeout.cancel(data[key].timeoutId);
- }
if (data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete)) {
_setTimeoutToRemove(key, ((options && options.maxAge) || config.maxAge));
}
}
- _saveCacheConfig();
-
- if (config.storageMode) {
- storage.setItem(prefix + '.data.' + key, angular.toJson(data[key]));
- }
+ _syncToStorage(key);
if (size > config.capacity) {
this.remove(staleEnd.key);
@@ -630,6 +635,8 @@ Source: angular-cache-1.1.0.js
_refresh(lruEntry);
+ _syncToStorage(key);
+
return item.value;
};
@@ -657,7 +664,7 @@ Source: angular-cache-1.1.0.js
delete lruHash[key];
delete data[key];
- _saveCacheConfig();
+ _syncToStorage(null);
if (config.storageMode) {
storage.removeItem(prefix + '.data.' + key);
@@ -686,7 +693,7 @@ Source: angular-cache-1.1.0.js
staleEnd = null;
if (config.storageMode) {
- _saveCacheConfig();
+ _syncToStorage();
}
};
@@ -719,8 +726,21 @@ Source: angular-cache-1.1.0.js
* @returns {Object} stats Object containing information about this cache.
* @privileged
*/
- this.info = function () {
- return angular.extend({}, config, { size: size });
+ this.info = function (key) {
+ if (key in data) {
+ var info = {
+ timestamp: data[key].timestamp,
+ maxAge: data[key].maxAge || config.maxAge,
+ aggressiveDelete: data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete) || false,
+ isExpired: false
+ };
+ if (info.maxAge) {
+ info.isExpired = (new Date().getTime() - info.timestamp) > info.maxAge;
+ }
+ return info;
+ } else {
+ return angular.extend({}, config, { size: size });
+ }
};
/**
@@ -872,7 +892,7 @@ Modules
- Documentation generated by JSDoc 3.2.0-dev on Tue Sep 03 2013 12:54:34 GMT-0600 (MDT)
+ Documentation generated by JSDoc 3.2.0-dev on Sat Sep 21 2013 10:34:05 GMT-0600 (MDT)
diff --git a/docs/index.html b/docs/index.html
index 45d03b8..5e76661 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -1,162 +1,161 @@
-
-
-
-
- JSDoc: Index
-
-
-
-
-
-
-
-
-
-
-
-
-
Index
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- dist/angular-cache-1.1.0.js
-
-
-
-
-
-
-
-
-
-
-
angular-cache is a caching system that improves upon the capabilities of the
-$cacheFactory provided by AngularJS.
-
-
-
-
-
-
-
- Version:
-
-
-
-
-
-
-
-
-
-
- Author:
-
-
-
-
-
-
- Copyright:
-
-
-
-
- License:
-
-
-
-
-
-
- Source:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Modules Classes
-
-
-
-
-
- Documentation generated by JSDoc 3.2.0-dev on Tue Sep 03 2013 12:54:34 GMT-0600 (MDT)
-
-
-
-
-
+
+
+
+
+ JSDoc: Index
+
+
+
+
+
+
+
+
+
+
+
+
+
Index
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ dist/angular-cache-1.2.0.js
+
+
+
+
+
+
+
+
+
+
+
angular-cache is a caching system that improves upon the capabilities of the
$cacheFactory provided by AngularJS.
+
+
+
+
+
+
+
+ Version:
+
+
+
+
+
+
+
+
+
+
+ Author:
+
+
+
+
+
+
+ Copyright:
+
+
+
+
+ License:
+
+
+
+
+
+
+ Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Modules Classes
+
+
+
+
+
+ Documentation generated by JSDoc 3.2.0-dev on Sat Sep 21 2013 10:34:05 GMT-0600 (MDT)
+
+
+
+
+
diff --git a/docs/module-angular-cache.html b/docs/module-angular-cache.html
index 54b6dbd..65c7743 100644
--- a/docs/module-angular-cache.html
+++ b/docs/module-angular-cache.html
@@ -1,124 +1,121 @@
-
-
-
-
- JSDoc: Module: angular-cache
-
-
-
-
-
-
-
-
-
-
-
-
-
Module: angular-cache
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Provides an $AngularCacheFactoryProvider, which gives you the ability to use an
- $angularCacheFactory. The $angularCacheFactory produces AngularCache objects, which
- the same abilities as the cache objects that come with Angular, except with some added
- functionality.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Source:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Modules Classes
-
-
-
-
-
- Documentation generated by JSDoc 3.2.0-dev on Tue Sep 03 2013 12:54:35 GMT-0600 (MDT)
-
-
-
-
-
+
+
+
+
+ JSDoc: Module: angular-cache
+
+
+
+
+
+
+
+
+
+
+
+
+
Module: angular-cache
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Provides an $AngularCacheFactoryProvider, which gives you the ability to use an
$angularCacheFactory. The $angularCacheFactory produces AngularCache objects, which
the same abilities as the cache objects that come with Angular, except with some added
functionality.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Modules Classes
+
+
+
+
+
+ Documentation generated by JSDoc 3.2.0-dev on Sat Sep 21 2013 10:34:05 GMT-0600 (MDT)
+
+
+
+
+
diff --git a/karma.conf.js b/karma.conf.js
new file mode 100644
index 0000000..9ee9ad0
--- /dev/null
+++ b/karma.conf.js
@@ -0,0 +1,36 @@
+module.exports = function (config) {
+ config.set({
+ basePath: './',
+ frameworks: ['jasmine'],
+ autoWatch: true,
+ browsers: ['Chrome'],
+ captureTimeout: 60000,
+ colors: true,
+ coverageReporter: {
+ type: 'html',
+ dir: 'coverage/'
+ },
+ exclude: ['dist/'],
+ files: [
+ 'test/vendor/angular-1.1.5.min.js',
+ 'test/vendor/angular-mocks-1.1.5.js',
+ 'src/angular-cache.js',
+ 'test/*.js'
+ ],
+ logLevel: config.LOG_DEBUG,
+ port: 9876,
+ plugins: [
+ 'karma-chrome-launcher',
+ 'karma-firefox-launcher',
+ 'karma-phantomjs-launcher',
+ 'karma-jasmine',
+ 'karma-coverage'
+ ],
+ preprocessors: {
+ 'src/angular-cache.js': ['coverage']
+ },
+ reporters: ['progress', 'coverage'],
+ runnerPort: 9100,
+ singleRun: false
+ });
+};
\ No newline at end of file
diff --git a/package.json b/package.json
index dd387b4..66d3774 100644
--- a/package.json
+++ b/package.json
@@ -1,32 +1,41 @@
{
- "name": "angular-cache",
- "description": "angular-cache is a very useful replacement for Angular's $cacheFactory.",
- "version": "1.1.0",
- "homepage": "https://github.com/jmdobry/angular-cache",
- "repository": {
- "type": "git",
- "url": "git://github.com/jmdobry/angular-cache.git"
- },
- "author": {
- "name": "Jason Dobry",
- "url": "http://www.pseudobry.com",
- "email": "jason.dobry@gmail.com"
- },
- "devDependencies": {
- "grunt": "~0.4.1",
- "grunt-contrib-jshint": "~0.5.2",
- "grunt-contrib-clean": "~0.4.1",
- "grunt-contrib-uglify": "~0.2.0",
- "grunt-contrib-copy": "~0.4.1",
- "karma": "~0.8.5",
- "grunt-karma": "~0.4.4",
- "grunt-jsdoc": "~0.3.2",
- "grunt-cli": "~0.1.8",
- "phantomjs": "1.9.1-0"
- },
- "scripts": {
- "test": "node node_modules/grunt-cli/bin/grunt build",
- "test_drone": "node node_modules/grunt-cli/bin/grunt drone",
- "test_travis": "node node_modules/grunt-cli/bin/grunt travis"
- }
+ "name": "angular-cache",
+ "description": "angular-cache is a very useful replacement for Angular's $cacheFactory.",
+ "version": "1.2.0",
+ "homepage": "https://github.com/jmdobry/angular-cache",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jmdobry/angular-cache.git"
+ },
+ "author": {
+ "name": "Jason Dobry",
+ "url": "http://www.pseudobry.com",
+ "email": "jason.dobry@gmail.com"
+ },
+ "devDependencies": {
+ "grunt": "~0.4.1",
+ "grunt-contrib-jshint": "~0.5.2",
+ "grunt-contrib-clean": "~0.4.1",
+ "grunt-contrib-uglify": "~0.2.0",
+ "grunt-contrib-copy": "~0.4.1",
+ "karma": "~0.10.2",
+ "grunt-karma": "~0.6.2",
+ "grunt-jsdoc": "~0.3.2",
+ "grunt-cli": "~0.1.8",
+ "phantomjs": "1.9.1-0",
+ "karma-script-launcher": "~0.1.0",
+ "karma-phantomjs-launcher": "~0.1.0",
+ "karma-chrome-launcher": "~0.1.0",
+ "karma-firefox-launcher": "~0.1.0",
+ "karma-html2js-preprocessor": "~0.1.0",
+ "karma-jasmine": "~0.1.3",
+ "karma-requirejs": "~0.1.0",
+ "karma-coffee-preprocessor": "~0.1.0",
+ "karma-coverage": "~0.1.0"
+ },
+ "scripts": {
+ "test": "node node_modules/grunt-cli/bin/grunt build",
+ "test_drone": "node node_modules/grunt-cli/bin/grunt drone",
+ "test_travis": "node node_modules/grunt-cli/bin/grunt travis"
+ }
}
diff --git a/src/angular-cache.js b/src/angular-cache.js
index fd76470..5ec922e 100644
--- a/src/angular-cache.js
+++ b/src/angular-cache.js
@@ -298,7 +298,7 @@
if (!cacheDirty) {
_loadCacheConfig();
} else {
- _saveCacheConfig();
+ _syncToStorage(null);
keys = _keys(data);
for (i = 0; i < keys.length; i++) {
storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]]));
@@ -313,7 +313,7 @@
if (!cacheDirty) {
_loadCacheConfig();
} else {
- _saveCacheConfig();
+ _syncToStorage(null);
keys = _keys(data);
for (i = 0; i < keys.length; i++) {
storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]]));
@@ -470,22 +470,26 @@
if (data.hasOwnProperty('aggressiveDelete')) {
options.aggressiveDelete = data.aggressiveDelete;
}
- self.put(keys[i], data.value);
+ self.put(keys[i], data.value, options);
}
}
- _saveCacheConfig();
+ _syncToStorage(null, null);
}
}
/**
- * @method _saveCacheConfig
- * @desc If storageMode is set, save current keys of cache to localStorage.
+ * @method _syncToStorage
+ * @desc If storageMode is set, sync to localStorage.
+ * @param {String} key The identifier of the item to sync.
* @private
* @ignore
*/
- function _saveCacheConfig() {
+ function _syncToStorage(key) {
if (config.storageMode && storage) {
storage.setItem(prefix + '.keys', angular.toJson(_keys(data)));
+ if (key) {
+ storage.setItem(prefix + '.data.' + key, angular.toJson(data[key]));
+ }
}
}
@@ -494,7 +498,7 @@
* @desc Add a key-value pair with timestamp to the cache.
* @param {String} key The identifier for the item to add to the cache.
* @param {*} value The value of the item to add to the cache.
- * @param {Object} [options] { maxAge: {Number} }
+ * @param {Object} [options] {{ maxAge: {Number}, aggressiveDelete: {Boolean}, timestamp: {Number} }}
* @returns {*} value The value of the item added to the cache.
* @privileged
*/
@@ -525,12 +529,20 @@
if (!(key in data)) {
size++;
+ } else {
+ if (data[key].timeoutId) {
+ $timeout.cancel(data[key].timeoutId);
+ }
}
data[key] = {
value: value
};
+ if (options && options.maxAge) {
+ data[key].maxAge = options.maxAge;
+ }
+
if (options && options.hasOwnProperty('aggressiveDelete')) {
data[key].aggressiveDelete = options.aggressiveDelete;
}
@@ -538,19 +550,12 @@
data[key].timestamp = (options && options.timestamp) || new Date().getTime();
if ((options && options.maxAge) || config.maxAge) {
- if (data[key].timeoutId) {
- $timeout.cancel(data[key].timeoutId);
- }
if (data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete)) {
_setTimeoutToRemove(key, ((options && options.maxAge) || config.maxAge));
}
}
- _saveCacheConfig();
-
- if (config.storageMode) {
- storage.setItem(prefix + '.data.' + key, angular.toJson(data[key]));
- }
+ _syncToStorage(key);
if (size > config.capacity) {
this.remove(staleEnd.key);
@@ -603,6 +608,8 @@
_refresh(lruEntry);
+ _syncToStorage(key);
+
return item.value;
};
@@ -630,7 +637,7 @@
delete lruHash[key];
delete data[key];
- _saveCacheConfig();
+ _syncToStorage(null);
if (config.storageMode) {
storage.removeItem(prefix + '.data.' + key);
@@ -659,7 +666,7 @@
staleEnd = null;
if (config.storageMode) {
- _saveCacheConfig();
+ _syncToStorage();
}
};
@@ -692,8 +699,21 @@
* @returns {Object} stats Object containing information about this cache.
* @privileged
*/
- this.info = function () {
- return angular.extend({}, config, { size: size });
+ this.info = function (key) {
+ if (key in data) {
+ var info = {
+ timestamp: data[key].timestamp,
+ maxAge: data[key].maxAge || config.maxAge,
+ aggressiveDelete: data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete) || false,
+ isExpired: false
+ };
+ if (info.maxAge) {
+ info.isExpired = (new Date().getTime() - info.timestamp) > info.maxAge;
+ }
+ return info;
+ } else {
+ return angular.extend({}, config, { size: size });
+ }
};
/**
diff --git a/test/angular-cacheSpec.js b/test/angular-cacheSpec.js
deleted file mode 100644
index 0f28700..0000000
--- a/test/angular-cacheSpec.js
+++ /dev/null
@@ -1,1106 +0,0 @@
-describe('angular-cache', function () {
-
- var $angularCacheFactory, $timeout;
- beforeEach(module('angular-cache'));
- beforeEach(inject(function ($injector) {
- $angularCacheFactory = $injector.get('$angularCacheFactory');
- $timeout = $injector.get('$timeout');
- }));
-
- describe('$angularCacheFactory', function () {
- describe('$angularCacheFactory()', function () {
- it('should be able to create a default cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache).toBeDefined();
- expect(cache.info().id).toEqual('cache');
- expect(cache.info().capacity).toEqual(Number.MAX_VALUE);
- expect(cache.info().maxAge).toEqual(null);
- expect(cache.info().cacheFlushInterval).toEqual(null);
- cache.destroy();
- });
- it('should be able to create a cache with options', function () {
- var options = {
- capacity: Math.floor((Math.random() * 100000) + 1),
- maxAge: Math.floor((Math.random() * 100000) + 1),
- cacheFlushInterval: Math.floor((Math.random() * 100000) + 1),
- aggressiveDelete: true,
- storageMode: 'localStorage',
- localStorageImpl: {
- setItem: function () {},
- getItem: function () {},
- removeItem: function () {}
- },
- onExpire: function () {}
- };
- var cache = $angularCacheFactory('cache', options);
- expect(cache).toBeDefined();
- expect(cache.info().id).toEqual('cache');
- expect(cache.info().capacity).toEqual(options.capacity);
- expect(cache.info().maxAge).toEqual(options.maxAge);
- expect(cache.info().cacheFlushInterval).toEqual(options.cacheFlushInterval);
- expect(cache.info().aggressiveDelete).toEqual(options.aggressiveDelete);
- expect(cache.info().storageMode).toEqual(options.storageMode);
- expect(cache.info().localStorageImpl).not.toBeDefined(); // We don't expose this to the user
- expect(cache.info().onExpire).toEqual(options.onExpire);
- cache.destroy();
- expect($angularCacheFactory.get('cache')).not.toBeDefined();
- });
- it('should validate capacity', function () {
- var capacity = Math.floor((Math.random() * 100000) + 1) * -1;
- try {
- $angularCacheFactory('cache', { capacity: capacity });
- } catch (err) {
- var msg = err.message;
- }
- expect(msg).toEqual('capacity: must be greater than zero!');
- capacity = 'asdfasd';
- try {
- $angularCacheFactory('cache', { capacity: capacity });
- } catch (err) {
- msg = err.message;
- }
- expect(msg).toEqual('capacity: must be a number!');
- });
- it('should validate maxAge', function () {
- var maxAge = Math.floor((Math.random() * 100000) + 1) * -1;
- try {
- $angularCacheFactory('cache', { maxAge: maxAge });
- } catch (err) {
- var msg = err.message;
- }
- expect(msg).toEqual('maxAge: must be greater than zero!');
- maxAge = 'asdfasd';
- try {
- $angularCacheFactory('cache', { maxAge: maxAge });
- } catch (err) {
- msg = err.message;
- }
- expect(msg).toEqual('maxAge: must be a number!');
- });
- it('should validate cacheFlushInterval', function () {
- var cacheFlushInterval = Math.floor((Math.random() * 100000) + 1) * -1;
- try {
- $angularCacheFactory('cache', { cacheFlushInterval: cacheFlushInterval });
- } catch (err) {
- var msg = err.message;
- }
- expect(msg).toEqual('cacheFlushInterval: must be greater than zero!');
- cacheFlushInterval = 'asdfasd';
- try {
- $angularCacheFactory('cache', { cacheFlushInterval: cacheFlushInterval });
- } catch (err) {
- msg = err.message;
- }
- expect(msg).toEqual('cacheFlushInterval: must be a number!');
- });
- it('should prevent a cache from being duplicated', function () {
- try {
- $angularCacheFactory('cache');
- $angularCacheFactory('cache');
- } catch (err) {
- var msg = err.message;
- }
- expect(msg).toEqual('cacheId cache taken!');
- });
- it('should require cacheId to be a string', function () {
- var shouldBeAStringMsg = 'cacheId must be a string!';
- try {
- $angularCacheFactory(3);
- } catch (err) {
- var msg = err.message;
- }
- expect(msg).toEqual(shouldBeAStringMsg);
- try {
- $angularCacheFactory({obj: 'obj'});
- } catch (err) {
- msg = err.message;
- }
- expect(msg).toEqual(shouldBeAStringMsg);
- try {
- $angularCacheFactory(['obj']);
- } catch (err) {
- msg = err.message;
- }
- expect(msg).toEqual(shouldBeAStringMsg);
- });
- it('should load an existing cache from localStorage is storageMode was enabled', function () {
- if (localStorage) {
- localStorage.setItem('angular-cache.caches.lsCache.keys', angular.toJson(['item1', 'item2']));
- localStorage.setItem('angular-cache.caches.lsCache.data.item1', angular.toJson({
- value: 'value1',
- timestamp: new Date().getTime()
- }));
- localStorage.setItem('angular-cache.caches.lsCache.data.item2', angular.toJson({
- value: 'value2',
- timestamp: new Date().getTime()
- }));
- var lsCache = $angularCacheFactory('lsCache', { storageMode: 'localStorage', maxAge: 300, aggressiveDelete: true });
- expect(lsCache.get('item1')).toEqual('value1');
- expect(lsCache.get('item2')).toEqual('value2');
- waits(600);
- runs(function () {
- $timeout.flush();
- expect(lsCache.get('item1')).toEqual(null);
- expect(lsCache.get('item2')).toEqual(null);
- if (sessionStorage) {
- sessionStorage.setItem('angular-cache.caches.ssCache.keys', angular.toJson(['item1', 'item2']));
- sessionStorage.setItem('angular-cache.caches.ssCache.data.item1', angular.toJson({
- value: 'value1',
- timestamp: new Date().getTime()
- }));
- sessionStorage.setItem('angular-cache.caches.ssCache.data.item2', angular.toJson({
- value: 'value2',
- timestamp: new Date().getTime()
- }));
- var ssCache = $angularCacheFactory('ssCache', { storageMode: 'sessionStorage', maxAge: 300, aggressiveDelete: true });
- expect(ssCache.get('item1')).toEqual('value1');
- expect(ssCache.get('item2')).toEqual('value2');
- waits(600);
- runs(function () {
- $timeout.flush();
- expect(ssCache.get('item1')).toEqual(null);
- expect(ssCache.get('item2')).toEqual(null);
- lsCache.destroy();
- ssCache.destroy();
- });
- }
- });
- }
- });
- it('should allow use of custom localStorage/sessionStorage implementations', function () {
- var myLocalStorage = {
- localData: {},
- setItem: function (key, value) {
- this.localData[key] = value;
- },
- getItem: function (key) {
- return this.localData[key];
- },
- removeItem: function (key) {
- delete this.localData[key];
- }
- };
- var mySessionStorage = {
- localData: {},
- setItem: function (key, value) {
- this.localData[key] = value;
- },
- getItem: function (key) {
- return this.localData[key];
- },
- removeItem: function (key) {
- delete this.localData[key];
- }
- };
- myLocalStorage.setItem('angular-cache.caches.lsCache.keys', angular.toJson(['item1', 'item2']));
- myLocalStorage.setItem('angular-cache.caches.lsCache.data.item1', angular.toJson({
- value: 'value1',
- timestamp: new Date().getTime()
- }));
- myLocalStorage.setItem('angular-cache.caches.lsCache.data.item2', angular.toJson({
- value: 'value2',
- timestamp: new Date().getTime()
- }));
- var lsCache = $angularCacheFactory('lsCache', { localStorageImpl: myLocalStorage, storageMode: 'localStorage', maxAge: 300, aggressiveDelete: true });
- expect(lsCache.get('item1')).toEqual('value1');
- expect(lsCache.get('item2')).toEqual('value2');
- waits(600);
- runs(function () {
- $timeout.flush();
- expect(lsCache.get('item1')).toEqual(null);
- expect(lsCache.get('item2')).toEqual(null);
- mySessionStorage.setItem('angular-cache.caches.ssCache.keys', angular.toJson(['item1', 'item2']));
- mySessionStorage.setItem('angular-cache.caches.ssCache.data.item1', angular.toJson({
- value: 'value1',
- timestamp: new Date().getTime()
- }));
- mySessionStorage.setItem('angular-cache.caches.ssCache.data.item2', angular.toJson({
- value: 'value2',
- timestamp: new Date().getTime()
- }));
- var ssCache = $angularCacheFactory('ssCache', { sessionStorageImpl: mySessionStorage, storageMode: 'sessionStorage', maxAge: 300, aggressiveDelete: true });
- expect(ssCache.get('item1')).toEqual('value1');
- expect(ssCache.get('item2')).toEqual('value2');
- waits(600);
- runs(function () {
- $timeout.flush();
- expect(ssCache.get('item1')).toEqual(null);
- expect(ssCache.get('item2')).toEqual(null);
- lsCache.destroy();
- ssCache.destroy();
- });
- });
- });
- });
- describe('$angularCacheFactory.get(cachedId)', function () {
- it('should return the correct cache with the specified cacheId', function () {
- var cache = $angularCacheFactory('cache');
- expect($angularCacheFactory.get('cache')).toEqual(cache);
- cache.destroy();
- });
- it('should return \"undefined\" if the cache doesn\'t exist', function () {
- expect($angularCacheFactory.get('someNonExistentCache')).toEqual(undefined);
- });
- });
- describe('$angularCacheFactory.info()', function () {
- it('should return the correct info for each cache produced by the factory', function () {
- var options = {
- capacity: Math.floor((Math.random() * 100000) + 1),
- maxAge: Math.floor((Math.random() * 100000) + 1),
- cacheFlushInterval: Math.floor((Math.random() * 100000) + 1)
- };
-
- var cache = $angularCacheFactory('cache');
- var cache2 = $angularCacheFactory('cache2', {
- maxAge: options.maxAge
- });
- var cache3 = $angularCacheFactory('cache3', {
- capacity: options.capacity,
- cacheFlushInterval: options.cacheFlushInterval
- });
- var info = $angularCacheFactory.info();
- expect(info.cache.id).toEqual('cache');
- expect(info.cache.capacity).toEqual(Number.MAX_VALUE);
- expect(info.cache.size).toEqual(0);
-
- expect(info.cache2.id).toEqual('cache2');
- expect(info.cache2.capacity).toEqual(Number.MAX_VALUE);
- expect(info.cache2.size).toEqual(0);
-
- expect(info.cache3.id).toEqual('cache3');
- expect(info.cache3.cacheFlushInterval).toEqual(options.cacheFlushInterval);
- expect(info.cache3.capacity).toEqual(options.capacity);
- expect(info.cache3.size).toEqual(0);
- expect(info.cache3.cacheFlushIntervalId).toBeDefined();
- cache.destroy();
- cache2.destroy();
- cache3.destroy();
- });
- });
- describe('$angularCacheFactory.keySet()', function () {
- it('should return the correct set of keys associated the caches currently owned by the factory', function () {
- var cacheKeys = ['cache', 'cache1', 'cache2'];
-
- $angularCacheFactory(cacheKeys[0]);
- $angularCacheFactory(cacheKeys[1]);
- $angularCacheFactory(cacheKeys[2]);
-
- var keySet = $angularCacheFactory.keySet();
-
- expect(keySet.hasOwnProperty(cacheKeys[0])).toEqual(true);
- expect(keySet.hasOwnProperty(cacheKeys[1])).toEqual(true);
- expect(keySet.hasOwnProperty(cacheKeys[2])).toEqual(true);
-
- expect(keySet[cacheKeys[0]]).toEqual(cacheKeys[0]);
- expect(keySet[cacheKeys[1]]).toEqual(cacheKeys[1]);
- expect(keySet[cacheKeys[2]]).toEqual(cacheKeys[2]);
-
- $angularCacheFactory.get(cacheKeys[0]).destroy();
- $angularCacheFactory.get(cacheKeys[1]).destroy();
- $angularCacheFactory.get(cacheKeys[2]).destroy();
-
- keySet = $angularCacheFactory.keySet();
-
- expect(keySet.hasOwnProperty(cacheKeys[0])).toEqual(false);
- expect(keySet.hasOwnProperty(cacheKeys[1])).toEqual(false);
- expect(keySet.hasOwnProperty(cacheKeys[2])).toEqual(false);
- });
- });
- describe('$angularCacheFactory.keys()', function () {
- it('should return the correct array of keys associated the caches currently owned by the factory', function () {
- var cacheKeys = ['cache', 'cache1', 'cache2'];
-
- $angularCacheFactory(cacheKeys[0]);
- $angularCacheFactory(cacheKeys[1]);
- $angularCacheFactory(cacheKeys[2]);
-
- var keys = $angularCacheFactory.keys();
-
- expect(keys[0]).toEqual(cacheKeys[0]);
- expect(keys[1]).toEqual(cacheKeys[1]);
- expect(keys[2]).toEqual(cacheKeys[2]);
-
- $angularCacheFactory.get(cacheKeys[0]).destroy();
- $angularCacheFactory.get(cacheKeys[1]).destroy();
- $angularCacheFactory.get(cacheKeys[2]).destroy();
-
- keys = $angularCacheFactory.keys();
-
- expect(keys.length).toEqual(0);
- });
- });
- describe('$angularCacheFactory.removeAll()', function () {
- it('should call "destroy()" on all caches currently owned by the factory', function () {
- var cacheKeys = ['cache', 'cache1', 'cache2'];
-
- $angularCacheFactory(cacheKeys[0]);
- $angularCacheFactory(cacheKeys[1]);
- $angularCacheFactory(cacheKeys[2]);
-
- $angularCacheFactory.removeAll();
-
- expect($angularCacheFactory.get(cacheKeys[0])).not.toBeDefined();
- expect($angularCacheFactory.get(cacheKeys[1])).not.toBeDefined();
- expect($angularCacheFactory.get(cacheKeys[2])).not.toBeDefined();
- });
- });
- describe('$angularCacheFactory.clearAll()', function () {
- it('should call "removeAll()" on all caches currently owned by the factory', function () {
- var cacheKeys = ['cache', 'cache1', 'cache2'];
-
- var cache = $angularCacheFactory(cacheKeys[0]);
- cache.put('item', 'value');
- var cache1 = $angularCacheFactory(cacheKeys[1]);
- cache1.put('item', 'value');
- var cache2 = $angularCacheFactory(cacheKeys[2]);
- cache2.put('item', 'value');
-
- $angularCacheFactory.clearAll();
-
- expect(cache.get('item')).not.toBeDefined();
- expect(cache1.get('item')).not.toBeDefined();
- expect(cache2.get('item')).not.toBeDefined();
-
- $angularCacheFactory.get(cacheKeys[0]).destroy();
- $angularCacheFactory.get(cacheKeys[1]).destroy();
- $angularCacheFactory.get(cacheKeys[2]).destroy();
- });
- });
- });
-
- describe('AngularCache', function () {
- it('should clear itself if cacheFlushInterval is specified', function () {
- var cache = $angularCacheFactory('cache', { cacheFlushInterval: 1000 });
- cache.put('item1', 'value1');
- expect(cache.get('item1')).toEqual('value1');
- cache.put('item2', 'value2');
- expect(cache.get('item2')).toEqual('value2');
- waits(1500);
- runs(function () {
- expect(cache.get('item1')).toEqual(undefined);
- expect(cache.get('item2')).toEqual(undefined);
- cache.destroy();
- });
- });
- describe('AngularCache.put(key, value, options)', function () {
- it('should disallow keys that aren\'t a string', function () {
- var cache = $angularCacheFactory('cache');
- var mustBeAStringMsg = 'The key must be a string!';
- try {
- cache.put(2, 'value');
- } catch (err) {
- var errorMsg = err.message;
- }
- expect(errorMsg).toEqual(mustBeAStringMsg);
- try {
- cache.put(true, 'value');
- } catch (err) {
- errorMsg = err.message;
- }
- expect(errorMsg).toEqual(mustBeAStringMsg);
- try {
- cache.put({ obj: 'obj' }, 'value');
- } catch (err) {
- errorMsg = err.message;
- }
- expect(errorMsg).toEqual(mustBeAStringMsg);
- cache.destroy();
- });
- it('should not add values that aren\'t defined', function () {
- var cache = $angularCacheFactory('cache');
- cache.put('item', null);
- expect(cache.get('item')).toEqual(undefined);
- cache.put('item', undefined);
- expect(cache.get('item')).toEqual(undefined);
- cache.destroy();
- });
- it('should validate maxAge', function () {
- var cache = $angularCacheFactory('cache');
- try {
- cache.put('item', 'value', { maxAge: 'als;dlfkajsd'});
- } catch (err) {
- var errorMsg = err.message;
- }
- expect(errorMsg).toEqual('AngularCache.put(): maxAge: must be a number!');
- try {
- cache.put('item', 'value', { maxAge: Math.floor((Math.random() * 100000) + 1) * -1 });
- } catch (err) {
- errorMsg = err.message;
- }
- expect(errorMsg).toEqual('AngularCache.put(): maxAge: must be greater than zero!');
- errorMsg = null;
- try {
- cache.put('item', 'value', { maxAge: Math.floor((Math.random() * 100000) + 1) });
- } catch (err) {
- errorMsg = 'should not reach this!';
- }
- expect(errorMsg).toEqual(null);
- cache.destroy();
- });
- it('should increase the size of the cache by one', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.info().size).toEqual(0);
- cache.put('item', 'value1');
- expect(cache.info().size).toEqual(1);
- cache.put('item2', 'value2');
- expect(cache.info().size).toEqual(2);
- cache.destroy();
- });
- it('should overwrite an item if it is re-added to the cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.info().size).toEqual(0);
- cache.put('item', 'value1');
- expect(cache.info().size).toEqual(1);
- cache.put('item', 'value2');
- expect(cache.info().size).toEqual(1);
- expect(cache.get('item')).toEqual('value2');
- cache.destroy();
- });
- it('should remove the least recently used item if the capacity has been reached', function () {
- var cache = $angularCacheFactory('cache', { capacity: 2 });
- expect(cache.info().size).toEqual(0);
- cache.put('item1', 'value1');
- expect(cache.info().size).toEqual(1);
- cache.put('item2', 'value2');
- expect(cache.info().size).toEqual(2);
- cache.put('item3', 'value3');
- expect(cache.info().size).toEqual(2);
- expect(cache.get('item1')).toEqual(undefined);
- expect(cache.get('item2')).toEqual('value2');
- expect(cache.get('item3')).toEqual('value3');
- cache.get('item2');
- cache.put('item1', 'value1');
- expect(cache.get('item3')).toEqual(undefined);
- expect(cache.get('item1')).toEqual('value1');
- expect(cache.get('item2')).toEqual('value2');
- cache.destroy();
- });
- it('should set a timeout for an item to expire if maxAge and aggressiveDelete for cache are specified', function () {
- var cache = $angularCacheFactory('cache', { maxAge: 1000, aggressiveDelete: true });
- cache.put('item1', 'value1');
- expect(cache.get('item1')).toEqual('value1');
- waits(1500);
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).toEqual(undefined);
- cache.destroy();
- });
- });
- it('should should lazy delete an item when maxAge is specified and aggressiveDelete is false for cache', function () {
- var cache = $angularCacheFactory('cache', { maxAge: 1000 });
- cache.put('item1', 'value1');
- expect(cache.get('item1')).toEqual('value1');
- waits(1500);
- runs(function () {
- expect(cache.get('item1')).toEqual(undefined);
- cache.destroy();
- });
- });
- it('should set a timeout for an item to expire if maxAge for item is specified and aggressiveDelete is true', function () {
- var cache = $angularCacheFactory('cache');
- cache.put('item1', 'value1', { maxAge: 1000, aggressiveDelete: true });
- expect(cache.get('item1')).toEqual('value1');
- waits(1500);
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).toEqual(undefined);
- cache.destroy();
- });
- });
- it('maxAge for a specific item should override maxAge for the cache', function () {
- var cache = $angularCacheFactory('cache', { maxAge: 3000, aggressiveDelete: true });
- cache.put('item1', 'value1', { maxAge: 1000 });
- expect(cache.get('item1')).toEqual('value1');
- waits(1500);
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).toEqual(undefined);
- cache.destroy();
- });
- });
- it('aggressiveDelete false for a specific item should override aggressiveDelete true for the cache', function () {
- var cache = $angularCacheFactory('cache', { maxAge: 1000, aggressiveDelete: true });
- cache.put('item1', 'value1', { maxAge: 1000, aggressiveDelete: false });
- expect(cache.get('item1')).toEqual('value1');
- waits(1500);
- runs(function () {
- expect(cache.get('item1')).toEqual(undefined);
- cache.destroy();
- });
- });
- it('should save data to localStorage when storageMode is used', function () {
- var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
- sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
-
- localStorageCache.put('item1', 'value1');
- sessionStorageCache.put('item1', 'value1');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1"]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1"]');
- }
-
- localStorageCache.destroy();
- sessionStorageCache.destroy();
- });
- });
- describe('AngularCache.get(key)', function () {
- it('should return the correct value for the specified key', function () {
- var cache = $angularCacheFactory('cache');
- var value1 = 'value1',
- value2 = 2,
- value3 = {
- value3: 'stuff'
- };
- cache.put('item1', value1);
- cache.put('item2', value2);
- cache.put('item3', value3);
- expect(cache.get('item1')).toEqual(value1);
- expect(cache.get('item2')).toEqual(value2);
- expect(cache.get('item3')).toEqual(value3);
- cache.destroy();
- });
- it('should return undefined if the key isn\'t in the cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.get('item')).toEqual(undefined);
- cache.destroy();
- });
- it('should execute globally configured \'onExpire\' callback if the item is expired in passive mode and global \'onExpire\' callback is configured', function () {
- var cache = $angularCacheFactory('cache', {
- maxAge: 300,
- onExpire: function (key, value, done) {
- done(key, value, 'executed global callback');
- }
- });
- cache.put('item', 'value');
- waits(700);
- runs(function () {
- cache.get('item', function (key, value, test) {
- expect(key).toEqual('item');
- expect(value).toEqual('value');
- expect(test).toEqual('executed global callback');
- });
- cache.destroy();
- });
- });
- it('should execute globally configured \'onExpire\' callback when an item is aggressively deleted and global \'onExpire\' callback is configured', function () {
- var onExpire = jasmine.createSpy();
- var cache = $angularCacheFactory('cache', {
- maxAge: 300,
- aggressiveDelete: true,
- onExpire: onExpire
- });
- cache.put('item', 'value');
- waits(700);
- runs(function () {
- $timeout.flush();
- expect(onExpire).toHaveBeenCalled();
- expect(onExpire).toHaveBeenCalledWith('item', 'value');
- cache.destroy();
- });
- });
- it('should execute local \'onExpire\' callback if the item is expired in passive mode and global \'onExpire\' callback is NOT configured', function () {
- var cache = $angularCacheFactory('cache', {
- maxAge: 300
- });
- cache.put('item', 'value');
- waits(700);
- runs(function () {
- cache.get('item', function (key, value) {
- expect(key).toEqual('item');
- expect(value).toEqual('value');
- });
- cache.destroy();
- });
- });
- });
- describe('AngularCache.remove(key)', function () {
- it('should remove the item with the specified key', function () {
- var cache = $angularCacheFactory('cache');
- var value1 = 'value1',
- value2 = 2,
- value3 = {
- value3: 'stuff'
- };
- cache.put('item1', value1);
- cache.put('item2', value2);
- cache.put('item3', value3);
- cache.remove('item1');
- expect(cache.get('item1')).toEqual(undefined);
- cache.remove('item2');
- expect(cache.get('item2')).toEqual(undefined);
- cache.remove('item3');
- expect(cache.get('item3')).toEqual(undefined);
- cache.destroy();
- });
- it('should reduce the size of the cache by one if the size is greater than zero', function () {
- var cache = $angularCacheFactory('cache');
- cache.put('item1', 'value1');
- expect(cache.info().size).toEqual(1);
- cache.put('item2', 'value2');
- expect(cache.info().size).toEqual(2);
- cache.remove('item1');
- expect(cache.info().size).toEqual(1);
- cache.remove('item2');
- expect(cache.info().size).toEqual(0);
- cache.remove('item1');
- expect(cache.info().size).toEqual(0);
- cache.remove('item2');
- expect(cache.info().size).toEqual(0);
- cache.destroy();
- });
- it('should remove items from localStorage when storageMode is used', function () {
- var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
- sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
-
- localStorageCache.put('item1', 'value1');
- sessionStorageCache.put('item1', 'value1');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1"]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1"]');
- }
-
- localStorageCache.remove('item1');
- sessionStorageCache.remove('item1');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('[]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('[]');
- }
-
- localStorageCache.destroy();
- sessionStorageCache.destroy();
- });
- });
- describe('AngularCache.removeAll()', function () {
- it('should remove all items in the cache', function () {
- var cache = $angularCacheFactory('cache');
- var value1 = 'value1',
- value2 = 2,
- value3 = {
- value3: 'stuff'
- };
- cache.put('item1', value1);
- cache.put('item2', value2);
- cache.put('item3', value3);
- cache.removeAll();
- expect(cache.get('item1')).toEqual(undefined);
- expect(cache.get('item2')).toEqual(undefined);
- expect(cache.get('item3')).toEqual(undefined);
- cache.destroy();
- });
- it('should remove items from localStorage when storageMode is used', function () {
- var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
- sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
-
- localStorageCache.put('item1', 'value1');
- sessionStorageCache.put('item1', 'value1');
- localStorageCache.put('item2', 'value2');
- sessionStorageCache.put('item2', 'value2');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2')).value).toEqual('value2');
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1","item2"]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2')).value).toEqual('value2');
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1","item2"]');
- }
-
- localStorageCache.removeAll();
- sessionStorageCache.removeAll();
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2'))).toEqual(null);
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('[]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2'))).toEqual(null);
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('[]');
- }
-
- localStorageCache.destroy();
- sessionStorageCache.destroy();
- });
- });
- describe('AngularCache.destroy()', function () {
- it('should completely destroy the cache', function () {
- var cache = $angularCacheFactory('cache');
- cache.destroy();
- expect($angularCacheFactory.get('cache')).toEqual(undefined);
- });
- it('should remove items from localStorage when storageMode is used', function () {
- var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
- sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
-
- localStorageCache.put('item1', 'value1');
- sessionStorageCache.put('item1', 'value1');
- localStorageCache.put('item2', 'value2');
- sessionStorageCache.put('item2', 'value2');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2')).value).toEqual('value2');
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1","item2"]');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2')).value).toEqual('value2');
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1","item2"]');
- }
-
- localStorageCache.destroy();
- sessionStorageCache.destroy();
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2'))).toEqual(null);
- expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual(null);
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2'))).toEqual(null);
- expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual(null);
- }
- });
- });
- describe('AngularCache.info()', function () {
- it('should return the correct values', function () {
- var onExpire = function () {};
- var cache = $angularCacheFactory('cache'),
- cache2 = $angularCacheFactory('cache2', { maxAge: 1000 }),
- cache3 = $angularCacheFactory('cache3', { cacheFlushInterval: 1000 }),
- cache4 = $angularCacheFactory('cache4', { capacity: 1000 }),
- cache5 = $angularCacheFactory('cache5', { storageMode: 'localStorage' }),
- cache6 = $angularCacheFactory('cache6', { storageMode: 'sessionStorage' });
- cache7 = $angularCacheFactory('cache7', { maxAge: 100, onExpire: onExpire });
- expect(cache.info()).toEqual({
- id: 'cache',
- capacity: Number.MAX_VALUE,
- size: 0,
- maxAge: null,
- cacheFlushInterval: null,
- aggressiveDelete: false,
- storageMode: null
- });
- cache.put('item', 'value');
- expect(cache.info()).toEqual({
- id: 'cache',
- capacity: Number.MAX_VALUE,
- size: 1,
- maxAge: null,
- cacheFlushInterval: null,
- aggressiveDelete: false,
- storageMode: null
- });
- expect(cache2.info()).toEqual({
- id: 'cache2',
- capacity: Number.MAX_VALUE,
- maxAge: 1000,
- size: 0,
- cacheFlushInterval: null,
- aggressiveDelete: false,
- storageMode: null
- });
- expect(cache3.info().id).toEqual('cache3');
- expect(cache3.info().capacity).toEqual(Number.MAX_VALUE);
- expect(cache3.info().cacheFlushInterval).toEqual(1000);
- expect(cache3.info().size).toEqual(0);
- expect(cache4.info()).toEqual({
- id: 'cache4',
- capacity: 1000,
- size: 0,
- maxAge: null,
- cacheFlushInterval: null,
- aggressiveDelete: false,
- storageMode: null
- });
- if (localStorage) {
- expect(cache5.info().storageMode).toEqual('localStorage');
- } else {
- expect(cache5.info().storageMode).toEqual(null);
- }
- if (sessionStorage) {
- expect(cache6.info().storageMode).toEqual('sessionStorage');
- } else {
- expect(cache6.info().storageMode).toEqual(null);
- }
- expect(cache7.info().onExpire).toEqual(onExpire);
- cache.destroy();
- cache2.destroy();
- cache3.destroy();
- cache4.destroy();
- cache5.destroy();
- cache6.destroy();
- cache7.destroy();
- });
- });
- describe('AngularCache.keySet()', function () {
- it('should return the correct set of keys of all items currently in a cache', function () {
- var itemKeys = ['item1', 'item2', 'item3'];
-
- var cache = $angularCacheFactory('cache');
-
- cache.put(itemKeys[0], itemKeys[0]);
- cache.put(itemKeys[1], itemKeys[1]);
- cache.put(itemKeys[2], itemKeys[2]);
-
- var keySet = cache.keySet();
-
- expect(keySet.hasOwnProperty(itemKeys[0])).toEqual(true);
- expect(keySet.hasOwnProperty(itemKeys[1])).toEqual(true);
- expect(keySet.hasOwnProperty(itemKeys[2])).toEqual(true);
-
- expect(keySet[itemKeys[0]]).toEqual(itemKeys[0]);
- expect(keySet[itemKeys[1]]).toEqual(itemKeys[1]);
- expect(keySet[itemKeys[2]]).toEqual(itemKeys[2]);
-
- cache.remove(itemKeys[0]);
- cache.remove(itemKeys[1]);
- cache.remove(itemKeys[2]);
-
- keySet = cache.keySet();
-
- expect(keySet.hasOwnProperty(itemKeys[0])).toEqual(false);
- expect(keySet.hasOwnProperty(itemKeys[1])).toEqual(false);
- expect(keySet.hasOwnProperty(itemKeys[2])).toEqual(false);
- cache.destroy();
- });
- });
- describe('AngularCache.keys()', function () {
- it('should return the correct array of keys of all items currently in a cache', function () {
- var itemKeys = ['item1', 'item2', 'item3'];
-
- var cache = $angularCacheFactory('cache');
-
- cache.put(itemKeys[0], itemKeys[0]);
- cache.put(itemKeys[1], itemKeys[1]);
- cache.put(itemKeys[2], itemKeys[2]);
-
- var keys = cache.keys();
-
- expect(keys[0]).toEqual(itemKeys[0]);
- expect(keys[1]).toEqual(itemKeys[1]);
- expect(keys[2]).toEqual(itemKeys[2]);
-
- cache.remove(itemKeys[0]);
- cache.remove(itemKeys[1]);
- cache.remove(itemKeys[2]);
-
- keys = cache.keys();
-
- expect(keys.length).toEqual(0);
- cache.destroy();
- });
- });
- describe('AngularCache.setOptions()', function () {
- it('should correctly reset to defaults if strict mode is true', function () {
- var onExpire = function () {};
- var cache = $angularCacheFactory('cache', {
- maxAge: 100,
- cacheFlushInterval: 200,
- onExpire: onExpire,
- storageMode: 'localStorage'
- });
- expect(cache.info().maxAge).toEqual(100);
- expect(cache.info().cacheFlushInterval).toEqual(200);
- expect(cache.info().onExpire).toEqual(onExpire);
- expect(cache.info().storageMode).toEqual('localStorage');
- cache.setOptions({ }, true);
- expect(cache.info().maxAge).toEqual(null);
- expect(cache.info().cacheFlushInterval).toEqual(null);
- expect(cache.info().onExpire).toEqual(null);
- expect(cache.info().storageMode).toEqual(null);
- cache.destroy();
- });
- it('should correctly modify the capacity of a cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.info().capacity).toEqual(Number.MAX_VALUE);
- cache.setOptions({ capacity: 5 }, false);
- expect(cache.info().capacity).toEqual(5);
- cache.put('item1', 1);
- cache.put('item2', 2);
- cache.put('item3', 3);
- cache.put('item4', 4);
- cache.put('item5', 5);
- cache.put('item6', 6);
- expect(cache.get('item1')).not.toBeDefined();
- cache.setOptions({ capacity: 3 }, false);
- // Least-recently used items over the new capacity should have been removed.
- expect(cache.get('item2')).not.toBeDefined();
- expect(cache.get('item3')).not.toBeDefined();
- expect(cache.info().size).toEqual(3);
- cache.destroy();
- });
- it('should correctly modify the maxAge of a cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.info().maxAge).toEqual(null);
- cache.setOptions({ maxAge: 2000, aggressiveDelete: true }, false);
- expect(cache.info().maxAge).toEqual(2000);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(2200);
- // The first items should be removed after 2000 ms
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.setOptions({ maxAge: 500, aggressiveDelete: true }, false);
- expect(cache.info().maxAge).toEqual(500);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(700);
- // The new items should be removed after 500 ms (the new maxAge)
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.destroy();
- });
- });
- });
- it('should correctly modify the cacheFlushInterval of a cache', function () {
- var cache = $angularCacheFactory('cache');
- expect(cache.info().cacheFlushInterval).toEqual(null);
- cache.setOptions({ cacheFlushInterval: 2000 }, false);
- expect(cache.info().cacheFlushInterval).toEqual(2000);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(2200);
- // The first items should be removed after 2000 ms
- runs(function () {
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.setOptions({ cacheFlushInterval: 500 }, false);
- expect(cache.info().cacheFlushInterval).toEqual(500);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(700);
- // The new items should be removed after 500 ms (the new maxAge)
- runs(function () {
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.destroy();
- });
- });
- });
- it('should correctly modify the aggressiveDelete of a cache', function () {
- var cache = $angularCacheFactory('cache', { maxAge: 2000 });
- expect(cache.info().aggressiveDelete).toEqual(false);
- cache.setOptions({ aggressiveDelete: true }, false);
- expect(cache.info().aggressiveDelete).toEqual(true);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(2500);
- // The first items should be removed after 2000 ms
- runs(function () {
- $timeout.flush();
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.setOptions({ maxAge: 500, aggressiveDelete: false }, false);
- expect(cache.info().aggressiveDelete).toEqual(false);
- cache.put('item1', 1);
- cache.put('item2', 2);
- waits(700);
- // The new items should be removed after 500 ms (the new maxAge)
- runs(function () {
- expect(cache.get('item1')).not.toBeDefined();
- expect(cache.get('item2')).not.toBeDefined();
- cache.destroy();
- });
- });
- });
- it('should correctly set configuration to default when \'strict\' is true', function () {
- var cache = $angularCacheFactory('cache', {
- capacity: 10,
- maxAge: 1000,
- cacheFlushInterval: 1000,
- aggressiveDelete: true,
- storageMode: null
- });
- cache.setOptions({}, true);
- expect(cache.info()).toEqual({
- capacity: Number.MAX_VALUE,
- maxAge: null,
- cacheFlushInterval: null,
- id: 'cache',
- size: 0,
- aggressiveDelete: false,
- storageMode: null
- });
- });
- it('should correctly switch to using local/session storage when storageMode is activated', function () {
- var cache = $angularCacheFactory('cache'),
- cache2 = $angularCacheFactory('cache2');
- cache.put('item', 'value');
- cache2.put('item', 'value');
- cache.setOptions({ maxAge: 300, aggressiveDelete: true, storageMode: 'localStorage' });
- cache2.setOptions({ maxAge: 300, aggressiveDelete: true, storageMode: 'sessionStorage' });
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.cache.data.item')).value).toEqual('value');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).value).toEqual('value');
- }
- waits(600);
- runs(function () {
- $timeout.flush();
- expect(cache.get('item')).toEqual(null);
- expect(cache2.get('item')).toEqual(null);
- if (localStorage) {
- expect(localStorage.getItem('angular-cache.caches.cache.data.item')).toEqual(null);
- }
- if (sessionStorage) {
- expect(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).toEqual(null);
- }
- cache.destroy();
- cache2.destroy();
- });
- });
- it('should correctly stop using local/session storage when storageMode is deactivated', function () {
- var cache = $angularCacheFactory('cache', { storageMode: 'localStorage' }),
- cache2 = $angularCacheFactory('cache2', { storageMode: 'sessionStorage' });
- cache.put('item', 'value');
- cache2.put('item', 'value');
-
- if (localStorage) {
- expect(angular.fromJson(localStorage.getItem('angular-cache.caches.cache.data.item')).value).toEqual('value');
- }
- if (sessionStorage) {
- expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).value).toEqual('value');
- }
-
- cache.setOptions({ storageMode: null }, true);
- cache2.setOptions({ storageMode: null }, true);
-
- if (localStorage) {
- expect(localStorage.getItem('angular-cache.caches.cache.data.item')).toEqual(null);
- }
- if (sessionStorage) {
- expect(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).toEqual(null);
- }
-
- cache.destroy();
- cache2.destroy();
- });
- });
- });
-});
\ No newline at end of file
diff --git a/test/angularCacheFactorySpec.js b/test/angularCacheFactorySpec.js
new file mode 100644
index 0000000..e4978a4
--- /dev/null
+++ b/test/angularCacheFactorySpec.js
@@ -0,0 +1,378 @@
+describe('AngularCacheFactory', function () {
+
+ var $angularCacheFactory, $timeout;
+ beforeEach(module('angular-cache'));
+ beforeEach(inject(function ($injector) {
+ $angularCacheFactory = $injector.get('$angularCacheFactory');
+ $timeout = $injector.get('$timeout');
+ }));
+
+ describe('$angularCacheFactory()', function () {
+ it('should be able to create a default cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache).toBeDefined();
+ expect(cache.info().id).toEqual('cache');
+ expect(cache.info().capacity).toEqual(Number.MAX_VALUE);
+ expect(cache.info().maxAge).toEqual(null);
+ expect(cache.info().cacheFlushInterval).toEqual(null);
+ cache.destroy();
+ });
+ it('should be able to create a cache with options', function () {
+ var options = {
+ capacity: Math.floor((Math.random() * 100000) + 1),
+ maxAge: Math.floor((Math.random() * 100000) + 1),
+ cacheFlushInterval: Math.floor((Math.random() * 100000) + 1),
+ aggressiveDelete: true,
+ storageMode: 'localStorage',
+ localStorageImpl: {
+ setItem: function () {
+ },
+ getItem: function () {
+ },
+ removeItem: function () {
+ }
+ },
+ onExpire: function () {
+ }
+ };
+ var cache = $angularCacheFactory('cache', options);
+ expect(cache).toBeDefined();
+ expect(cache.info().id).toEqual('cache');
+ expect(cache.info().capacity).toEqual(options.capacity);
+ expect(cache.info().maxAge).toEqual(options.maxAge);
+ expect(cache.info().cacheFlushInterval).toEqual(options.cacheFlushInterval);
+ expect(cache.info().aggressiveDelete).toEqual(options.aggressiveDelete);
+ expect(cache.info().storageMode).toEqual(options.storageMode);
+ expect(cache.info().localStorageImpl).not.toBeDefined(); // We don't expose this to the user
+ expect(cache.info().onExpire).toEqual(options.onExpire);
+ cache.destroy();
+ expect($angularCacheFactory.get('cache')).not.toBeDefined();
+ });
+ it('should validate capacity', function () {
+ var capacity = Math.floor((Math.random() * 100000) + 1) * -1;
+ try {
+ $angularCacheFactory('cache', { capacity: capacity });
+ } catch (err) {
+ var msg = err.message;
+ }
+ expect(msg).toEqual('capacity: must be greater than zero!');
+ capacity = 'asdfasd';
+ try {
+ $angularCacheFactory('cache', { capacity: capacity });
+ } catch (err) {
+ msg = err.message;
+ }
+ expect(msg).toEqual('capacity: must be a number!');
+ });
+ it('should validate maxAge', function () {
+ var maxAge = Math.floor((Math.random() * 100000) + 1) * -1;
+ try {
+ $angularCacheFactory('cache', { maxAge: maxAge });
+ } catch (err) {
+ var msg = err.message;
+ }
+ expect(msg).toEqual('maxAge: must be greater than zero!');
+ maxAge = 'asdfasd';
+ try {
+ $angularCacheFactory('cache', { maxAge: maxAge });
+ } catch (err) {
+ msg = err.message;
+ }
+ expect(msg).toEqual('maxAge: must be a number!');
+ });
+ it('should validate cacheFlushInterval', function () {
+ var cacheFlushInterval = Math.floor((Math.random() * 100000) + 1) * -1;
+ try {
+ $angularCacheFactory('cache', { cacheFlushInterval: cacheFlushInterval });
+ } catch (err) {
+ var msg = err.message;
+ }
+ expect(msg).toEqual('cacheFlushInterval: must be greater than zero!');
+ cacheFlushInterval = 'asdfasd';
+ try {
+ $angularCacheFactory('cache', { cacheFlushInterval: cacheFlushInterval });
+ } catch (err) {
+ msg = err.message;
+ }
+ expect(msg).toEqual('cacheFlushInterval: must be a number!');
+ });
+ it('should prevent a cache from being duplicated', function () {
+ try {
+ $angularCacheFactory('cache');
+ $angularCacheFactory('cache');
+ } catch (err) {
+ var msg = err.message;
+ }
+ expect(msg).toEqual('cacheId cache taken!');
+ });
+ it('should require cacheId to be a string', function () {
+ var shouldBeAStringMsg = 'cacheId must be a string!';
+ try {
+ $angularCacheFactory(3);
+ } catch (err) {
+ var msg = err.message;
+ }
+ expect(msg).toEqual(shouldBeAStringMsg);
+ try {
+ $angularCacheFactory({obj: 'obj'});
+ } catch (err) {
+ msg = err.message;
+ }
+ expect(msg).toEqual(shouldBeAStringMsg);
+ try {
+ $angularCacheFactory(['obj']);
+ } catch (err) {
+ msg = err.message;
+ }
+ expect(msg).toEqual(shouldBeAStringMsg);
+ });
+ it('should load an existing cache from localStorage is storageMode was enabled', function () {
+ if (localStorage) {
+ localStorage.setItem('angular-cache.caches.lsCache.keys', angular.toJson(['lsItem1', 'lsItem2']));
+ localStorage.setItem('angular-cache.caches.lsCache.data.lsItem1', angular.toJson({
+ value: 'value1',
+ timestamp: new Date().getTime()
+ }));
+ localStorage.setItem('angular-cache.caches.lsCache.data.lsItem2', angular.toJson({
+ value: 'value2',
+ timestamp: new Date().getTime(),
+ maxAge: 10000
+ }));
+ var lsCache = $angularCacheFactory('lsCache', { storageMode: 'localStorage', maxAge: 10, aggressiveDelete: true });
+ expect(lsCache.get('lsItem1')).toEqual('value1');
+ localStorage.removeItem('angular-cache.caches.lsCache.data.lsItem1');
+ expect(localStorage.getItem('lsItem1')).toEqual(null);
+ expect(lsCache.get('lsItem1')).toEqual('value1');
+ expect(localStorage.getItem('lsItem1')).toBeDefined();
+ expect(lsCache.get('lsItem2')).toEqual('value2');
+ expect(lsCache.info('lsItem2').maxAge).toEqual(10000);
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(lsCache.get('lsItem1')).toEqual(null);
+ expect(lsCache.get('lsItem2')).toEqual(null);
+ if (sessionStorage) {
+ sessionStorage.setItem('angular-cache.caches.ssCache.keys', angular.toJson(['ssItem1', 'ssItem2']));
+ sessionStorage.setItem('angular-cache.caches.ssCache.data.ssItem1', angular.toJson({
+ value: 'value1',
+ timestamp: new Date().getTime()
+ }));
+ sessionStorage.setItem('angular-cache.caches.ssCache.data.ssItem2', angular.toJson({
+ value: 'value2',
+ timestamp: new Date().getTime()
+ }));
+ var ssCache = $angularCacheFactory('ssCache', { storageMode: 'sessionStorage', maxAge: 10, aggressiveDelete: true });
+ expect(ssCache.get('ssItem1')).toEqual('value1');
+ expect(ssCache.get('ssItem2')).toEqual('value2');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(ssCache.get('ssItem1')).toEqual(null);
+ expect(ssCache.get('ssItem2')).toEqual(null);
+ lsCache.destroy();
+ ssCache.destroy();
+ });
+ }
+ });
+ }
+ });
+ it('should allow use of custom localStorage/sessionStorage implementations', function () {
+ var myLocalStorage = {
+ localData: {},
+ setItem: function (key, value) {
+ this.localData[key] = value;
+ },
+ getItem: function (key) {
+ return this.localData[key];
+ },
+ removeItem: function (key) {
+ delete this.localData[key];
+ }
+ };
+ var mySessionStorage = {
+ localData: {},
+ setItem: function (key, value) {
+ this.localData[key] = value;
+ },
+ getItem: function (key) {
+ return this.localData[key];
+ },
+ removeItem: function (key) {
+ delete this.localData[key];
+ }
+ };
+ myLocalStorage.setItem('angular-cache.caches.lsCache.keys', angular.toJson(['item1', 'item2']));
+ myLocalStorage.setItem('angular-cache.caches.lsCache.data.item1', angular.toJson({
+ value: 'value1',
+ timestamp: new Date().getTime()
+ }));
+ myLocalStorage.setItem('angular-cache.caches.lsCache.data.item2', angular.toJson({
+ value: 'value2',
+ timestamp: new Date().getTime()
+ }));
+ var lsCache = $angularCacheFactory('lsCache', { localStorageImpl: myLocalStorage, storageMode: 'localStorage', maxAge: 10, aggressiveDelete: true });
+ expect(lsCache.get('item1')).toEqual('value1');
+ expect(lsCache.get('item2')).toEqual('value2');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(lsCache.get('item1')).toEqual(null);
+ expect(lsCache.get('item2')).toEqual(null);
+ mySessionStorage.setItem('angular-cache.caches.ssCache.keys', angular.toJson(['item1', 'item2']));
+ mySessionStorage.setItem('angular-cache.caches.ssCache.data.item1', angular.toJson({
+ value: 'value1',
+ timestamp: new Date().getTime()
+ }));
+ mySessionStorage.setItem('angular-cache.caches.ssCache.data.item2', angular.toJson({
+ value: 'value2',
+ timestamp: new Date().getTime()
+ }));
+ var ssCache = $angularCacheFactory('ssCache', { sessionStorageImpl: mySessionStorage, storageMode: 'sessionStorage', maxAge: 10, aggressiveDelete: true });
+ expect(ssCache.get('item1')).toEqual('value1');
+ expect(ssCache.get('item2')).toEqual('value2');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(ssCache.get('item1')).toEqual(null);
+ expect(ssCache.get('item2')).toEqual(null);
+ lsCache.destroy();
+ ssCache.destroy();
+ });
+ });
+ });
+ });
+ describe('$angularCacheFactory.get(cachedId)', function () {
+ it('should return the correct cache with the specified cacheId', function () {
+ var cache = $angularCacheFactory('cache');
+ expect($angularCacheFactory.get('cache')).toEqual(cache);
+ cache.destroy();
+ });
+ it('should return \"undefined\" if the cache doesn\'t exist', function () {
+ expect($angularCacheFactory.get('someNonExistentCache')).toEqual(undefined);
+ });
+ });
+ describe('$angularCacheFactory.info()', function () {
+ it('should return the correct info for each cache produced by the factory', function () {
+ var options = {
+ capacity: Math.floor((Math.random() * 100000) + 1),
+ maxAge: Math.floor((Math.random() * 100000) + 1),
+ cacheFlushInterval: Math.floor((Math.random() * 100000) + 1)
+ };
+
+ var cache = $angularCacheFactory('cache');
+ var cache2 = $angularCacheFactory('cache2', {
+ maxAge: options.maxAge
+ });
+ var cache3 = $angularCacheFactory('cache3', {
+ capacity: options.capacity,
+ cacheFlushInterval: options.cacheFlushInterval
+ });
+ var info = $angularCacheFactory.info();
+ expect(info.cache.id).toEqual('cache');
+ expect(info.cache.capacity).toEqual(Number.MAX_VALUE);
+ expect(info.cache.size).toEqual(0);
+
+ expect(info.cache2.id).toEqual('cache2');
+ expect(info.cache2.capacity).toEqual(Number.MAX_VALUE);
+ expect(info.cache2.size).toEqual(0);
+
+ expect(info.cache3.id).toEqual('cache3');
+ expect(info.cache3.cacheFlushInterval).toEqual(options.cacheFlushInterval);
+ expect(info.cache3.capacity).toEqual(options.capacity);
+ expect(info.cache3.size).toEqual(0);
+ expect(info.cache3.cacheFlushIntervalId).toBeDefined();
+ cache.destroy();
+ cache2.destroy();
+ cache3.destroy();
+ });
+ });
+ describe('$angularCacheFactory.keySet()', function () {
+ it('should return the correct set of keys associated the caches currently owned by the factory', function () {
+ var cacheKeys = ['cache', 'cache1', 'cache2'];
+
+ $angularCacheFactory(cacheKeys[0]);
+ $angularCacheFactory(cacheKeys[1]);
+ $angularCacheFactory(cacheKeys[2]);
+
+ var keySet = $angularCacheFactory.keySet();
+
+ expect(keySet.hasOwnProperty(cacheKeys[0])).toEqual(true);
+ expect(keySet.hasOwnProperty(cacheKeys[1])).toEqual(true);
+ expect(keySet.hasOwnProperty(cacheKeys[2])).toEqual(true);
+
+ expect(keySet[cacheKeys[0]]).toEqual(cacheKeys[0]);
+ expect(keySet[cacheKeys[1]]).toEqual(cacheKeys[1]);
+ expect(keySet[cacheKeys[2]]).toEqual(cacheKeys[2]);
+
+ $angularCacheFactory.get(cacheKeys[0]).destroy();
+ $angularCacheFactory.get(cacheKeys[1]).destroy();
+ $angularCacheFactory.get(cacheKeys[2]).destroy();
+
+ keySet = $angularCacheFactory.keySet();
+
+ expect(keySet.hasOwnProperty(cacheKeys[0])).toEqual(false);
+ expect(keySet.hasOwnProperty(cacheKeys[1])).toEqual(false);
+ expect(keySet.hasOwnProperty(cacheKeys[2])).toEqual(false);
+ });
+ });
+ describe('$angularCacheFactory.keys()', function () {
+ it('should return the correct array of keys associated the caches currently owned by the factory', function () {
+ var cacheKeys = ['cache', 'cache1', 'cache2'];
+
+ $angularCacheFactory(cacheKeys[0]);
+ $angularCacheFactory(cacheKeys[1]);
+ $angularCacheFactory(cacheKeys[2]);
+
+ var keys = $angularCacheFactory.keys();
+
+ expect(keys[0]).toEqual(cacheKeys[0]);
+ expect(keys[1]).toEqual(cacheKeys[1]);
+ expect(keys[2]).toEqual(cacheKeys[2]);
+
+ $angularCacheFactory.get(cacheKeys[0]).destroy();
+ $angularCacheFactory.get(cacheKeys[1]).destroy();
+ $angularCacheFactory.get(cacheKeys[2]).destroy();
+
+ keys = $angularCacheFactory.keys();
+
+ expect(keys.length).toEqual(0);
+ });
+ });
+ describe('$angularCacheFactory.removeAll()', function () {
+ it('should call "destroy()" on all caches currently owned by the factory', function () {
+ var cacheKeys = ['cache', 'cache1', 'cache2'];
+
+ $angularCacheFactory(cacheKeys[0]);
+ $angularCacheFactory(cacheKeys[1]);
+ $angularCacheFactory(cacheKeys[2]);
+
+ $angularCacheFactory.removeAll();
+
+ expect($angularCacheFactory.get(cacheKeys[0])).not.toBeDefined();
+ expect($angularCacheFactory.get(cacheKeys[1])).not.toBeDefined();
+ expect($angularCacheFactory.get(cacheKeys[2])).not.toBeDefined();
+ });
+ });
+ describe('$angularCacheFactory.clearAll()', function () {
+ it('should call "removeAll()" on all caches currently owned by the factory', function () {
+ var cacheKeys = ['cache', 'cache1', 'cache2'];
+
+ var cache = $angularCacheFactory(cacheKeys[0]);
+ cache.put('item', 'value');
+ var cache1 = $angularCacheFactory(cacheKeys[1]);
+ cache1.put('item', 'value');
+ var cache2 = $angularCacheFactory(cacheKeys[2]);
+ cache2.put('item', 'value');
+
+ $angularCacheFactory.clearAll();
+
+ expect(cache.get('item')).not.toBeDefined();
+ expect(cache1.get('item')).not.toBeDefined();
+ expect(cache2.get('item')).not.toBeDefined();
+
+ $angularCacheFactory.get(cacheKeys[0]).destroy();
+ $angularCacheFactory.get(cacheKeys[1]).destroy();
+ $angularCacheFactory.get(cacheKeys[2]).destroy();
+ });
+ });
+});
\ No newline at end of file
diff --git a/test/angularCacheSpec.js b/test/angularCacheSpec.js
new file mode 100644
index 0000000..c0848ff
--- /dev/null
+++ b/test/angularCacheSpec.js
@@ -0,0 +1,754 @@
+describe('AngularCache', function () {
+
+ var $angularCacheFactory, $timeout;
+ beforeEach(module('angular-cache'));
+ beforeEach(inject(function ($injector) {
+ $angularCacheFactory = $injector.get('$angularCacheFactory');
+ $timeout = $injector.get('$timeout');
+ }));
+
+ it('should clear itself if cacheFlushInterval is specified', function () {
+ var cache = $angularCacheFactory('cache', { cacheFlushInterval: 10 });
+ cache.put('item1', 'value1');
+ expect(cache.get('item1')).toEqual('value1');
+ cache.put('item2', 'value2');
+ expect(cache.get('item2')).toEqual('value2');
+ waits(100);
+ runs(function () {
+ expect(cache.get('item1')).toEqual(undefined);
+ expect(cache.get('item2')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ describe('AngularCache.put(key, value, options)', function () {
+ it('should disallow keys that aren\'t a string', function () {
+ var cache = $angularCacheFactory('cache');
+ var mustBeAStringMsg = 'The key must be a string!';
+ try {
+ cache.put(2, 'value');
+ } catch (err) {
+ var errorMsg = err.message;
+ }
+ expect(errorMsg).toEqual(mustBeAStringMsg);
+ try {
+ cache.put(true, 'value');
+ } catch (err) {
+ errorMsg = err.message;
+ }
+ expect(errorMsg).toEqual(mustBeAStringMsg);
+ try {
+ cache.put({ obj: 'obj' }, 'value');
+ } catch (err) {
+ errorMsg = err.message;
+ }
+ expect(errorMsg).toEqual(mustBeAStringMsg);
+ cache.destroy();
+ });
+ it('should not add values that aren\'t defined', function () {
+ var cache = $angularCacheFactory('cache');
+ cache.put('item', null);
+ expect(cache.get('item')).toEqual(undefined);
+ cache.put('item', undefined);
+ expect(cache.get('item')).toEqual(undefined);
+ cache.destroy();
+ });
+ it('should validate maxAge', function () {
+ var cache = $angularCacheFactory('cache');
+ try {
+ cache.put('item', 'value', { maxAge: 'als;dlfkajsd'});
+ } catch (err) {
+ var errorMsg = err.message;
+ }
+ expect(errorMsg).toEqual('AngularCache.put(): maxAge: must be a number!');
+ try {
+ cache.put('item', 'value', { maxAge: Math.floor((Math.random() * 100000) + 1) * -1 });
+ } catch (err) {
+ errorMsg = err.message;
+ }
+ expect(errorMsg).toEqual('AngularCache.put(): maxAge: must be greater than zero!');
+ errorMsg = null;
+ try {
+ cache.put('item', 'value', { maxAge: Math.floor((Math.random() * 100000) + 1) });
+ } catch (err) {
+ errorMsg = 'should not reach this!';
+ }
+ expect(errorMsg).toEqual(null);
+ cache.destroy();
+ });
+ it('should increase the size of the cache by one', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.info().size).toEqual(0);
+ cache.put('item', 'value1');
+ expect(cache.info().size).toEqual(1);
+ cache.put('item2', 'value2');
+ expect(cache.info().size).toEqual(2);
+ cache.destroy();
+ });
+ it('should overwrite an item if it is re-added to the cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.info().size).toEqual(0);
+ cache.put('item', 'value1');
+ expect(cache.info().size).toEqual(1);
+ cache.put('item', 'value2');
+ expect(cache.info().size).toEqual(1);
+ expect(cache.get('item')).toEqual('value2');
+ cache.destroy();
+ });
+ it('should remove the least recently used item if the capacity has been reached', function () {
+ var cache = $angularCacheFactory('cache', { capacity: 2 });
+ expect(cache.info().size).toEqual(0);
+ cache.put('item1', 'value1');
+ expect(cache.info().size).toEqual(1);
+ cache.put('item2', 'value2');
+ expect(cache.info().size).toEqual(2);
+ cache.put('item3', 'value3');
+ expect(cache.info().size).toEqual(2);
+ expect(cache.get('item1')).toEqual(undefined);
+ expect(cache.get('item2')).toEqual('value2');
+ expect(cache.get('item3')).toEqual('value3');
+ cache.get('item2');
+ cache.put('item1', 'value1');
+ expect(cache.get('item3')).toEqual(undefined);
+ expect(cache.get('item1')).toEqual('value1');
+ expect(cache.get('item2')).toEqual('value2');
+ cache.destroy();
+ });
+ it('should set a timeout for an item to expire if maxAge and aggressiveDelete for cache are specified', function () {
+ var cache = $angularCacheFactory('cache', { maxAge: 10, aggressiveDelete: true });
+ cache.put('item1', 'value1');
+ expect(cache.get('item1')).toEqual('value1');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ it('should should lazy delete an item when maxAge is specified and aggressiveDelete is false for cache', function () {
+ var cache = $angularCacheFactory('cache', { maxAge: 10 });
+ cache.put('item1', 'value1');
+ expect(cache.get('item1')).toEqual('value1');
+ waits(100);
+ runs(function () {
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ it('should set a timeout for an item to expire if maxAge for item is specified and aggressiveDelete is true', function () {
+ var cache = $angularCacheFactory('cache');
+ cache.put('item1', 'value1', { maxAge: 10, aggressiveDelete: true });
+ expect(cache.get('item1')).toEqual('value1');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ it('maxAge for a specific item should override maxAge for the cache', function () {
+ var cache = $angularCacheFactory('cache', { maxAge: 1000, aggressiveDelete: true });
+ cache.put('item1', 'value1', { maxAge: 5 });
+ expect(cache.get('item1')).toEqual('value1');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ it('aggressiveDelete false for a specific item should override aggressiveDelete true for the cache', function () {
+ var cache = $angularCacheFactory('cache', { maxAge: 10, aggressiveDelete: true });
+ cache.put('item1', 'value1', { maxAge: 10, aggressiveDelete: false });
+ expect(cache.get('item1')).toEqual('value1');
+ waits(100);
+ runs(function () {
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.destroy();
+ });
+ });
+ it('should save data to localStorage when storageMode is used', function () {
+ var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
+ sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
+
+ localStorageCache.put('item1', 'value1');
+ sessionStorageCache.put('item1', 'value1');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1"]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1"]');
+ }
+
+ localStorageCache.destroy();
+ sessionStorageCache.destroy();
+ });
+ });
+ describe('AngularCache.get(key)', function () {
+ it('should return the correct value for the specified key', function () {
+ var cache = $angularCacheFactory('cache');
+ var value1 = 'value1',
+ value2 = 2,
+ value3 = {
+ value3: 'stuff'
+ };
+ cache.put('item1', value1);
+ cache.put('item2', value2);
+ cache.put('item3', value3);
+ expect(cache.get('item1')).toEqual(value1);
+ expect(cache.get('item2')).toEqual(value2);
+ expect(cache.get('item3')).toEqual(value3);
+ cache.destroy();
+ });
+ it('should return undefined if the key isn\'t in the cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.get('item')).toEqual(undefined);
+ cache.destroy();
+ });
+ it('should execute globally configured \'onExpire\' callback if the item is expired in passive mode and global \'onExpire\' callback is configured', function () {
+ var cache = $angularCacheFactory('cache', {
+ maxAge: 10,
+ onExpire: function (key, value, done) {
+ done(key, value, 'executed global callback');
+ }
+ });
+ cache.put('item', 'value');
+ waits(100);
+ runs(function () {
+ cache.get('item', function (key, value, test) {
+ expect(key).toEqual('item');
+ expect(value).toEqual('value');
+ expect(test).toEqual('executed global callback');
+ });
+ cache.destroy();
+ });
+ });
+ it('should execute globally configured \'onExpire\' callback when an item is aggressively deleted and global \'onExpire\' callback is configured', function () {
+ var onExpire = jasmine.createSpy();
+ var cache = $angularCacheFactory('cache', {
+ maxAge: 10,
+ aggressiveDelete: true,
+ onExpire: onExpire
+ });
+ cache.put('item', 'value');
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(onExpire).toHaveBeenCalled();
+ expect(onExpire).toHaveBeenCalledWith('item', 'value');
+ cache.destroy();
+ });
+ });
+ it('should execute local \'onExpire\' callback if the item is expired in passive mode and global \'onExpire\' callback is NOT configured', function () {
+ var cache = $angularCacheFactory('cache', {
+ maxAge: 10
+ });
+ cache.put('item', 'value');
+ waits(100);
+ runs(function () {
+ cache.get('item', function (key, value) {
+ expect(key).toEqual('item');
+ expect(value).toEqual('value');
+ });
+ cache.destroy();
+ });
+ });
+ });
+ describe('AngularCache.remove(key)', function () {
+ it('should remove the item with the specified key', function () {
+ var cache = $angularCacheFactory('cache');
+ var value1 = 'value1',
+ value2 = 2,
+ value3 = {
+ value3: 'stuff'
+ };
+ cache.put('item1', value1);
+ cache.put('item2', value2);
+ cache.put('item3', value3);
+ cache.remove('item1');
+ expect(cache.get('item1')).toEqual(undefined);
+ cache.remove('item2');
+ expect(cache.get('item2')).toEqual(undefined);
+ cache.remove('item3');
+ expect(cache.get('item3')).toEqual(undefined);
+ cache.destroy();
+ });
+ it('should reduce the size of the cache by one if the size is greater than zero', function () {
+ var cache = $angularCacheFactory('cache');
+ cache.put('item1', 'value1');
+ expect(cache.info().size).toEqual(1);
+ cache.put('item2', 'value2');
+ expect(cache.info().size).toEqual(2);
+ cache.remove('item1');
+ expect(cache.info().size).toEqual(1);
+ cache.remove('item2');
+ expect(cache.info().size).toEqual(0);
+ cache.remove('item1');
+ expect(cache.info().size).toEqual(0);
+ cache.remove('item2');
+ expect(cache.info().size).toEqual(0);
+ cache.destroy();
+ });
+ it('should remove items from localStorage when storageMode is used', function () {
+ var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
+ sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
+
+ localStorageCache.put('item1', 'value1');
+ sessionStorageCache.put('item1', 'value1');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1"]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1"]');
+ }
+
+ localStorageCache.remove('item1');
+ sessionStorageCache.remove('item1');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('[]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('[]');
+ }
+
+ localStorageCache.destroy();
+ sessionStorageCache.destroy();
+ });
+ });
+ describe('AngularCache.removeAll()', function () {
+ it('should remove all items in the cache', function () {
+ var cache = $angularCacheFactory('cache');
+ var value1 = 'value1',
+ value2 = 2,
+ value3 = {
+ value3: 'stuff'
+ };
+ cache.put('item1', value1);
+ cache.put('item2', value2);
+ cache.put('item3', value3);
+ cache.removeAll();
+ expect(cache.get('item1')).toEqual(undefined);
+ expect(cache.get('item2')).toEqual(undefined);
+ expect(cache.get('item3')).toEqual(undefined);
+ cache.destroy();
+ });
+ it('should remove items from localStorage when storageMode is used', function () {
+ var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
+ sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
+
+ localStorageCache.put('item1', 'value1');
+ sessionStorageCache.put('item1', 'value1');
+ localStorageCache.put('item2', 'value2');
+ sessionStorageCache.put('item2', 'value2');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2')).value).toEqual('value2');
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1","item2"]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2')).value).toEqual('value2');
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1","item2"]');
+ }
+
+ localStorageCache.removeAll();
+ sessionStorageCache.removeAll();
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2'))).toEqual(null);
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('[]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2'))).toEqual(null);
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('[]');
+ }
+
+ localStorageCache.destroy();
+ sessionStorageCache.destroy();
+ });
+ });
+ describe('AngularCache.destroy()', function () {
+ it('should completely destroy the cache', function () {
+ var cache = $angularCacheFactory('cache');
+ cache.destroy();
+ expect($angularCacheFactory.get('cache')).toEqual(undefined);
+ });
+ it('should remove items from localStorage when storageMode is used', function () {
+ var localStorageCache = $angularCacheFactory('localStorageCache', { storageMode: 'localStorage' }),
+ sessionStorageCache = $angularCacheFactory('sessionStorageCache', { storageMode: 'sessionStorage' });
+
+ localStorageCache.put('item1', 'value1');
+ sessionStorageCache.put('item1', 'value1');
+ localStorageCache.put('item2', 'value2');
+ sessionStorageCache.put('item2', 'value2');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1')).value).toEqual('value1');
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2')).value).toEqual('value2');
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual('["item1","item2"]');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1')).value).toEqual('value1');
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2')).value).toEqual('value2');
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual('["item1","item2"]');
+ }
+
+ localStorageCache.destroy();
+ sessionStorageCache.destroy();
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item1'))).toEqual(null);
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.localStorageCache.data.item2'))).toEqual(null);
+ expect(localStorage.getItem('angular-cache.caches.localStorageCache.keys')).toEqual(null);
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item1'))).toEqual(null);
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.data.item2'))).toEqual(null);
+ expect(sessionStorage.getItem('angular-cache.caches.sessionStorageCache.keys')).toEqual(null);
+ }
+ });
+ });
+ describe('AngularCache.info()', function () {
+ it('should return the correct values', function () {
+ var onExpire = function () {
+ };
+ var cache = $angularCacheFactory('cache'),
+ cache2 = $angularCacheFactory('cache2', { maxAge: 1000 }),
+ cache3 = $angularCacheFactory('cache3', { cacheFlushInterval: 1000 }),
+ cache4 = $angularCacheFactory('cache4', { capacity: 1000 }),
+ cache5 = $angularCacheFactory('cache5', { storageMode: 'localStorage' }),
+ cache6 = $angularCacheFactory('cache6', { storageMode: 'sessionStorage' });
+ cache7 = $angularCacheFactory('cache7', { maxAge: 100, onExpire: onExpire });
+ expect(cache.info()).toEqual({
+ id: 'cache',
+ capacity: Number.MAX_VALUE,
+ size: 0,
+ maxAge: null,
+ cacheFlushInterval: null,
+ aggressiveDelete: false,
+ storageMode: null
+ });
+ cache.put('item', 'value');
+ cache.put('item2', 'value2', { maxAge: 200, aggressiveDelete: true });
+
+ // AngularCache#info(key)
+ expect(typeof cache.info('item').timestamp).toEqual('number');
+ expect(cache.info('item').maxAge).toEqual(null);
+ expect(cache.info('item').aggressiveDelete).toEqual(false);
+ expect(typeof cache.info('item2').timestamp).toEqual('number');
+ expect(cache.info('item2').maxAge).toEqual(200);
+ expect(cache.info('item2').aggressiveDelete).toEqual(true);
+
+ expect(cache.info()).toEqual({
+ id: 'cache',
+ capacity: Number.MAX_VALUE,
+ size: 2,
+ maxAge: null,
+ cacheFlushInterval: null,
+ aggressiveDelete: false,
+ storageMode: null
+ });
+ expect(cache2.info()).toEqual({
+ id: 'cache2',
+ capacity: Number.MAX_VALUE,
+ maxAge: 1000,
+ size: 0,
+ cacheFlushInterval: null,
+ aggressiveDelete: false,
+ storageMode: null
+ });
+ expect(cache3.info().id).toEqual('cache3');
+ expect(cache3.info().capacity).toEqual(Number.MAX_VALUE);
+ expect(cache3.info().cacheFlushInterval).toEqual(1000);
+ expect(cache3.info().size).toEqual(0);
+ expect(cache4.info()).toEqual({
+ id: 'cache4',
+ capacity: 1000,
+ size: 0,
+ maxAge: null,
+ cacheFlushInterval: null,
+ aggressiveDelete: false,
+ storageMode: null
+ });
+ if (localStorage) {
+ expect(cache5.info().storageMode).toEqual('localStorage');
+ } else {
+ expect(cache5.info().storageMode).toEqual(null);
+ }
+ if (sessionStorage) {
+ expect(cache6.info().storageMode).toEqual('sessionStorage');
+ } else {
+ expect(cache6.info().storageMode).toEqual(null);
+ }
+ expect(cache7.info().onExpire).toEqual(onExpire);
+ cache.destroy();
+ cache2.destroy();
+ cache3.destroy();
+ cache4.destroy();
+ cache5.destroy();
+ cache6.destroy();
+ cache7.destroy();
+ });
+ });
+ describe('AngularCache.keySet()', function () {
+ it('should return the correct set of keys of all items currently in a cache', function () {
+ var itemKeys = ['item1', 'item2', 'item3'];
+
+ var cache = $angularCacheFactory('cache');
+
+ cache.put(itemKeys[0], itemKeys[0]);
+ cache.put(itemKeys[1], itemKeys[1]);
+ cache.put(itemKeys[2], itemKeys[2]);
+
+ var keySet = cache.keySet();
+
+ expect(keySet.hasOwnProperty(itemKeys[0])).toEqual(true);
+ expect(keySet.hasOwnProperty(itemKeys[1])).toEqual(true);
+ expect(keySet.hasOwnProperty(itemKeys[2])).toEqual(true);
+
+ expect(keySet[itemKeys[0]]).toEqual(itemKeys[0]);
+ expect(keySet[itemKeys[1]]).toEqual(itemKeys[1]);
+ expect(keySet[itemKeys[2]]).toEqual(itemKeys[2]);
+
+ cache.remove(itemKeys[0]);
+ cache.remove(itemKeys[1]);
+ cache.remove(itemKeys[2]);
+
+ keySet = cache.keySet();
+
+ expect(keySet.hasOwnProperty(itemKeys[0])).toEqual(false);
+ expect(keySet.hasOwnProperty(itemKeys[1])).toEqual(false);
+ expect(keySet.hasOwnProperty(itemKeys[2])).toEqual(false);
+ cache.destroy();
+ });
+ });
+ describe('AngularCache.keys()', function () {
+ it('should return the correct array of keys of all items currently in a cache', function () {
+ var itemKeys = ['item1', 'item2', 'item3'];
+
+ var cache = $angularCacheFactory('cache');
+
+ cache.put(itemKeys[0], itemKeys[0]);
+ cache.put(itemKeys[1], itemKeys[1]);
+ cache.put(itemKeys[2], itemKeys[2]);
+
+ var keys = cache.keys();
+
+ expect(keys[0]).toEqual(itemKeys[0]);
+ expect(keys[1]).toEqual(itemKeys[1]);
+ expect(keys[2]).toEqual(itemKeys[2]);
+
+ cache.remove(itemKeys[0]);
+ cache.remove(itemKeys[1]);
+ cache.remove(itemKeys[2]);
+
+ keys = cache.keys();
+
+ expect(keys.length).toEqual(0);
+ cache.destroy();
+ });
+ });
+ describe('AngularCache.setOptions()', function () {
+ it('should correctly reset to defaults if strict mode is true', function () {
+ var onExpire = function () {
+ };
+ var cache = $angularCacheFactory('cache', {
+ maxAge: 100,
+ cacheFlushInterval: 200,
+ onExpire: onExpire,
+ storageMode: 'localStorage'
+ });
+ expect(cache.info().maxAge).toEqual(100);
+ expect(cache.info().cacheFlushInterval).toEqual(200);
+ expect(cache.info().onExpire).toEqual(onExpire);
+ expect(cache.info().storageMode).toEqual('localStorage');
+ cache.setOptions({ }, true);
+ expect(cache.info().maxAge).toEqual(null);
+ expect(cache.info().cacheFlushInterval).toEqual(null);
+ expect(cache.info().onExpire).toEqual(null);
+ expect(cache.info().storageMode).toEqual(null);
+ cache.destroy();
+ });
+ it('should correctly modify the capacity of a cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.info().capacity).toEqual(Number.MAX_VALUE);
+ cache.setOptions({ capacity: 5 }, false);
+ expect(cache.info().capacity).toEqual(5);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ cache.put('item3', 3);
+ cache.put('item4', 4);
+ cache.put('item5', 5);
+ cache.put('item6', 6);
+ expect(cache.get('item1')).not.toBeDefined();
+ cache.setOptions({ capacity: 3 }, false);
+ // Least-recently used items over the new capacity should have been removed.
+ expect(cache.get('item2')).not.toBeDefined();
+ expect(cache.get('item3')).not.toBeDefined();
+ expect(cache.info().size).toEqual(3);
+ cache.destroy();
+ });
+ it('should correctly modify the maxAge of a cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.info().maxAge).toEqual(null);
+ cache.setOptions({ maxAge: 10, aggressiveDelete: true }, false);
+ expect(cache.info().maxAge).toEqual(10);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.setOptions({ maxAge: 10, aggressiveDelete: true }, false);
+ expect(cache.info().maxAge).toEqual(10);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ // The new items should be removed after 500 ms (the new maxAge)
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.destroy();
+ });
+ });
+ });
+ it('should correctly modify the cacheFlushInterval of a cache', function () {
+ var cache = $angularCacheFactory('cache');
+ expect(cache.info().cacheFlushInterval).toEqual(null);
+ cache.setOptions({ cacheFlushInterval: 10 }, false);
+ expect(cache.info().cacheFlushInterval).toEqual(10);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ // The first items should be removed after 2000 ms
+ runs(function () {
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.setOptions({ cacheFlushInterval: 10 }, false);
+ expect(cache.info().cacheFlushInterval).toEqual(10);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ // The new items should be removed after 500 ms (the new maxAge)
+ runs(function () {
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.destroy();
+ });
+ });
+ });
+ it('should correctly modify the aggressiveDelete of a cache', function () {
+ var cache = $angularCacheFactory('cache', { maxAge: 10 });
+ expect(cache.info().aggressiveDelete).toEqual(false);
+ cache.setOptions({ aggressiveDelete: true }, false);
+ expect(cache.info().aggressiveDelete).toEqual(true);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ // The first items should be removed after 2000 ms
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.setOptions({ maxAge: 10, aggressiveDelete: false }, false);
+ expect(cache.info().aggressiveDelete).toEqual(false);
+ cache.put('item1', 1);
+ cache.put('item2', 2);
+ waits(100);
+ // The new items should be removed after 500 ms (the new maxAge)
+ runs(function () {
+ expect(cache.get('item1')).not.toBeDefined();
+ expect(cache.get('item2')).not.toBeDefined();
+ cache.destroy();
+ });
+ });
+ });
+ it('should correctly set configuration to default when \'strict\' is true', function () {
+ var cache = $angularCacheFactory('cache', {
+ capacity: 10,
+ maxAge: 1000,
+ cacheFlushInterval: 1000,
+ aggressiveDelete: true,
+ storageMode: null
+ });
+ cache.setOptions({}, true);
+ expect(cache.info()).toEqual({
+ capacity: Number.MAX_VALUE,
+ maxAge: null,
+ cacheFlushInterval: null,
+ id: 'cache',
+ size: 0,
+ aggressiveDelete: false,
+ storageMode: null
+ });
+ });
+ it('should correctly switch to using local/session storage when storageMode is activated', function () {
+ var cache = $angularCacheFactory('cache'),
+ cache2 = $angularCacheFactory('cache2');
+ cache.put('item', 'value');
+ cache2.put('item', 'value');
+ cache.setOptions({ maxAge: 10, aggressiveDelete: true, storageMode: 'localStorage' });
+ cache2.setOptions({ maxAge: 10, aggressiveDelete: true, storageMode: 'sessionStorage' });
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.cache.data.item')).value).toEqual('value');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).value).toEqual('value');
+ }
+ waits(100);
+ runs(function () {
+ $timeout.flush();
+ expect(cache.get('item')).toEqual(null);
+ expect(cache2.get('item')).toEqual(null);
+ if (localStorage) {
+ expect(localStorage.getItem('angular-cache.caches.cache.data.item')).toEqual(null);
+ }
+ if (sessionStorage) {
+ expect(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).toEqual(null);
+ }
+ cache.destroy();
+ cache2.destroy();
+ });
+ });
+ it('should correctly stop using local/session storage when storageMode is deactivated', function () {
+ var cache = $angularCacheFactory('cache', { storageMode: 'localStorage' }),
+ cache2 = $angularCacheFactory('cache2', { storageMode: 'sessionStorage' });
+ cache.put('item', 'value');
+ cache2.put('item', 'value');
+
+ if (localStorage) {
+ expect(angular.fromJson(localStorage.getItem('angular-cache.caches.cache.data.item')).value).toEqual('value');
+ }
+ if (sessionStorage) {
+ expect(angular.fromJson(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).value).toEqual('value');
+ }
+
+ cache.setOptions({ storageMode: null }, true);
+ cache2.setOptions({ storageMode: null }, true);
+
+ if (localStorage) {
+ expect(localStorage.getItem('angular-cache.caches.cache.data.item')).toEqual(null);
+ }
+ if (sessionStorage) {
+ expect(sessionStorage.getItem('angular-cache.caches.cache2.data.item')).toEqual(null);
+ }
+
+ cache.destroy();
+ cache2.destroy();
+ });
+ });
+});
\ No newline at end of file
diff --git a/test/karma.conf.js b/test/karma.conf.js
deleted file mode 100644
index d86cfcb..0000000
--- a/test/karma.conf.js
+++ /dev/null
@@ -1,73 +0,0 @@
-// Karma configuration
-// Generated on Tue May 21 2013 18:52:40 GMT-0600 (Mountain Daylight Time)
-
-// base path, that will be used to resolve files and exclude
-basePath = '../';
-
-// list of files / patterns to load in the browser
-files = [
- JASMINE,
- JASMINE_ADAPTER,
- 'test/vendor/angular-1.1.5.min.js',
- 'test/vendor/angular-mocks-1.1.5.js',
- 'src/angular-cache.js',
- 'test/angular-cacheSpec.js'
-];
-
-// list of files to exclude
-exclude = [
- 'dist/'
-];
-
-// test results reporter to use
-// possible values: 'dots', 'progress', 'junit'
-reporters = ['progress', 'coverage'];
-
-preprocessors = {
- 'src/angular-cache.js': 'coverage'
-};
-
-coverageReporter = {
- type: 'html',
- dir: 'coverage/'
-};
-
-// web server port
-port = 9876;
-
-// cli runner port
-runnerPort = 9100;
-
-// enable / disable colors in the output (reporters and logs)
-colors = true;
-
-// level of logging
-// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
-logLevel = LOG_INFO;
-
-// enable / disable watching file and executing tests whenever any file changes
-autoWatch = true;
-
-var os = require('os');
-if (os.platform() !== 'win32') {
- process.env.PHANTOMJS_BIN = './node_modules/phantomjs/lib/phantom/bin/phantomjs';
-} else {
- process.env.PHANTOMJS_BIN = './node_modules/phantomjs/lib/phantom/phantomjs.exe';
-}
-
-// Start these browsers, currently available:
-// - Chrome
-// - ChromeCanary
-// - Firefox
-// - Opera
-// - Safari (only Mac)
-// - PhantomJS
-// - IE (only Windows)
-browsers = ['Chrome'];
-
-// If browser does not capture in given timeout [ms], kill it
-captureTimeout = 60000;
-
-// Continuous Integration mode
-// if true, it capture browsers, run tests and exit
-singleRun = false;