diff --git a/CHANGELOG.md b/CHANGELOG.md index e81deb1..999bad2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - Changed `$angularCacheFactory.info()` to return an object similar to `AngularCache.info()` #45 - Namespaced angular-cache module under `jmdobry` so it is now "jmdobry.angular-cache". #42 +###### Backwards compatible API changes +- Added ability to set global cache defaults in $angularCacheFactoryProvider. #55 + ###### Backwards compatible bug fixes - cacheFlushInterval doesn't clear web storage when storageMode is used. #52 - AngularCache#info(key) should return 'undefined' if the key isn't in the cache #53 diff --git a/README.md b/README.md index eb27213..ab49799 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ app.service('myService', function ($angularCacheFactory) { ### Table of Contents 1. [Demo](http://jmdobry.github.io/angular-cache/demo/) 1. [Configuration Parameters](#configuration) +1. [$angularCacheFactoryProvider](#angularcachefactoryprovider) 1. [$angularCacheFactory](#angularcachefactory) 1. [AngularCache](#angularcache) 1. [Status](#status) @@ -251,6 +252,47 @@ newCache.put('denver', 'broncos'); newCache.get('denver'); // 'broncos' or whatever was returned by the server in the "onExpire" callback ``` + +## $angularCacheFactoryProvider + +__Description:__ Provider for `$angularCacheFactory`. + +#### $angularCacheFactoryProvider.setCacheDefaults() +__Description:__ Set the default configuration for all caches created by `$angularCacheFactory`. + +__Usage:__ +```javascript +app.module('app', ['jmdobry.angular-cache']) + .config(function ($angularCacheFactoryProvider) { + $angularCacheFactoryProvider.setCacheDefaults({ + maxAge: 360000, + deleteOnExpire: 'aggressive' + }); + }) + .run(function ($angularCacheFactory) { + var info = $angularCacheFactory.info(); + + console.log(info.cacheDefaults); // output below + /* + { + capacity: Number.MAX_VALUE, + maxAge: 360000, + deleteOnExpire: 'aggressive', + onExpire: null, + cacheFlushInterval: null, + storageMode: 'none', + localStorageImpl: null, + sessionStorageImpl: null + } + */ + + var newCache = $angularCacheFactory('newCache'); + + newCache.info().maxAge; // 360000 + newCache.info().deleteOnExpire; // "aggressive" + }); +``` + ## $angularCacheFactory @@ -803,6 +845,9 @@ See [AngularCache#info](http://jmdobry.github.io/angular-cache/docs/Cache.html#i - Changed `$angularCacheFactory.info()` to return an object similar to `AngularCache.info()` #45 - Namespaced angular-cache module under `jmdobry` so it is now "jmdobry.angular-cache". #42 +###### Backwards compatible API changes +- Added ability to set global cache defaults in $angularCacheFactoryProvider. #55 + ###### Backwards compatible bug fixes - cacheFlushInterval doesn't clear web storage when storageMode is used. #52 - AngularCache#info(key) should return 'undefined' if the key isn't in the cache #53 diff --git a/src/angular-cache.js b/src/angular-cache.js index 5f9dd52..5c6625c 100644 --- a/src/angular-cache.js +++ b/src/angular-cache.js @@ -27,12 +27,9 @@ */ function $AngularCacheFactoryProvider() { - /** - * @ignore - */ - this.$get = ['$timeout', '$window', function ($timeout, $window) { - var caches = {}, - defaults = { + var cacheDefaults, + DEFAULTS = function () { + return { capacity: Number.MAX_VALUE, maxAge: null, deleteOnExpire: 'none', @@ -42,6 +39,25 @@ localStorageImpl: null, sessionStorageImpl: null }; + }; + + /** + * @method setCacheDefaults + * @desc Set the default configuration for all caches created by $angularCacheFactory. + * @param {Object} options + */ + this.setCacheDefaults = function (options) { + cacheDefaults = angular.extend({}, DEFAULTS(), options); + }; + + // Initialize cacheDefaults with the defaults + this.setCacheDefaults({}); + + /** + * @ignore + */ + this.$get = ['$timeout', '$window', function ($timeout, $window) { + var caches = {}; /** * @method _keySet @@ -328,7 +344,7 @@ } if (strict) { - options = angular.extend({}, defaults, options); + options = angular.extend({}, cacheDefaults, options); } if ('capacity' in options) { @@ -749,6 +765,7 @@ var key = keys[i]; info.caches[key] = caches[key].info(); } + info.cacheDefaults = cacheDefaults; return info; };