Skip to content

Commit

Permalink
Added ability to set global cache defaults in $angularCacheFactoryPro…
Browse files Browse the repository at this point in the history
…vider. #55
  • Loading branch information
jmdobry committed Sep 23, 2013
1 parent ec6bca7 commit 75672b0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -251,6 +252,47 @@ newCache.put('denver', 'broncos');
newCache.get('denver'); // 'broncos' or whatever was returned by the server in the "onExpire" callback
```

<a name='angularcachefactoryprovider'></a>
## $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"
});
```

<a name='angularcachefactory'></a>
## $angularCacheFactory

Expand Down Expand Up @@ -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
Expand Down
31 changes: 24 additions & 7 deletions src/angular-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -328,7 +344,7 @@
}

if (strict) {
options = angular.extend({}, defaults, options);
options = angular.extend({}, cacheDefaults, options);
}

if ('capacity' in options) {
Expand Down Expand Up @@ -749,6 +765,7 @@
var key = keys[i];
info.caches[key] = caches[key].info();
}
info.cacheDefaults = cacheDefaults;
return info;
};

Expand Down

0 comments on commit 75672b0

Please sign in to comment.