angular-cache is a caching system that improves upon the capabilities of the $cacheFactory provided by AngularJS. With angular-cache your caches can periodically clear themselves and flush items that have expired.
The goal of the project is to solve a general problem, not satisfy a specific scenario.
(These are in addition to what Angular's $cacheFactory provides.)
Set a default maximum lifetime on all items added to the cache. They will be removed when they expire. Can be configured on a per-item basis for greater specificity.
e.g. $angularCacheFactory('newCache', { maxAge: 36000 })
:
Set the cache to periodically clear itself.
e.g. $angularCacheFactory('newCache', { cacheFlushInterval: 57908 })
Return the set of keys associated with all current caches owned by $angularCacheFactory.
e.g. $angularCacheFactory.keySet()
Return the set of keys associated with all current items in someCache
.
e.g. $angularCacheFactory.get('someCache').keySet()
:
Return an array of the keys associated with all current caches owned by $angularCacheFactory.
e.g. $angularCacheFactory.keys()
Return an array of the keys associated with all current items in someCache
.
e.g. $angularCacheFactory.get('someCache').keys()
Dynamically configure a cache.
e.g. $angularCacheFactory.get('someCache').setOptions({ capacity: 4500 })
Version | Branch | Build status | Test Coverage |
---|---|---|---|
0.8.2 | master | Test Coverage | |
0.8.2 | develop | ||
0.8.2 | all |
or
Type | From drone.io | From raw.github.com | Size |
---|---|---|---|
Production | angular-cache-0.8.2.min.js | angular-cache-0.8.2.min.js | 3.3 KB |
Development | angular-cache-0.8.2.js | angular-cache-0.8.2.js | 28.7 KB |
- Load angular-cache
- Create a cache
- Dynamically configure a cache
- Retrieve a cache
- Retrieve items
- Add items
- Remove items
- Clear all items
- Destroy a cache
- Get info about a cache
- API Documentation
Make sure angular-cache is included on your web page after Angular.
angular.module('myApp', ['angular-cache']);
See angular-cache
angular.module('myApp').service('myService', ['$angularCacheFactory',
function ($angularCacheFactory) {
// create a cache with default settings
var myCache = $angularCacheFactory('myCache');
// create an LRU cache with a capacity of 10
var myLRUCache = $angularCacheFactory('myLRUCache', {
capacity: 10
});
// create a cache whose items have a maximum lifetime of 10 minutes
var myTimeLimitedCache = $angularCacheFactory('myTimeLimitedCache', {
maxAge: 600000
});
// create a cache that will clear itself every 10 minutes
var myIntervalCache = $angularCacheFactory('myIntervalCache', {
cacheFlushInterval: 600000
});
// create an cache with all options
var myAwesomeCache = $angularCacheFactory('myAwesomeCache', {
capacity: 10,
maxAge: 600000,
cacheFlushInterval: 600000
});
}
]);
angular.module('myApp').service('myService', ['$angularCacheFactory',
function ($angularCacheFactory) {
// create a cache with default settings
var cache = $angularCacheFactory('cache', {
capacity: 100,
maxAge: 30000
});
// Add 50 items here, for example
cache.info(); // { ..., size: 50, capacity: 100, maxAge: 3000, ... }
cache.setOptions({
capacity: 30
});
cache.info(); // { ..., size: 30, capacity: 30, maxAge: 3000, ... }
// notice that only the 30 most recently added items remain in the cache because
// the capacity was reduced.
// setting the second parameter to true will cause the cache's configuration to be
// reset to defaults before the configuration passed into setOptions() is applied to
// the cache
cache.setOptions({
cacheFlushInterval: 5500
}, true);
cache.info(); // { ..., size: 30, cacheFlushInterval: 5500,
// capacity: 1.7976931348623157e+308, maxAge: null, ... }
}
]);
angular.module('myApp').service('myOtherService', ['$angularCacheFactory',
function ($angularCacheFactory) {
var myCache = $angularCacheFactory.get('myCache');
}
]);
myCache.get('someItem'); // { name: 'John Doe' });
// if the item is not in the cache or has expired
myCache.get('someMissingItem'); // undefined
See AngularCache#get
myCache.put('someItem', { name: 'John Doe' });
myCache.get('someItem'); // { name: 'John Doe' });
Give a specific item a maximum age
// The maxAge given to this item will override the maxAge of the cache, if any was set
myCache.put('someItem', { name: 'John Doe' }, { maxAge: 10000 });
myCache.get('someItem'); // { name: 'John Doe' });
// wait at least ten seconds
setTimeout(function() {
myCache.get('someItem'); // undefined
}, 15000); // 15 seconds
See AngularCache#put
myCache.put('someItem', { name: 'John Doe' });
myCache.remove('someItem');
myCache.get('someItem'); // undefined
myCache.put('someItem', { name: 'John Doe' });
myCache.put('someOtherItem', { name: 'Sally Jean' });
myCache.removeAll();
myCache.get('someItem'); // undefined
myCache.get('someOtherItem'); // undefined
myCache.destroy();
myCache.get('someItem'); // Will throw an error - Don't try to use a cache after destroying it!
$angularCacheFactory.get('myCache'); // undefined
myCache.info(); // { id: 'myCache', size: 13 }
- Bug fixes
- Documentation tidy up.
- Yay!
- Make sure you aren't submitting a duplicate issue.
- Carefully describe how to reproduce the problem.
- Expect prompt feedback.
- Checkout a new branch based on
develop
and name it to what you intend to do:- Example:
$ git checkout -b BRANCH_NAME
- Use one branch per fix/feature
- Prefix your branch name with
feature-
orfix-
appropriately.
- Example:
- Make your changes
- Make sure to provide a spec for unit tests
- Run your tests with either
karma start
orgrunt test
- Make sure the tests pass
- Commit your changes
- Please provide a git message which explains what you've done
- Commit to the forked repository
- Make a pull request
- Make sure you send the PR to the
develop
branch - Travis CI is watching you!
- Make sure you send the PR to the
Read the detailed Contributing Guide
Copyright (C) 2013 Jason Dobry
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.