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.
// Angular's provided $cacheFactory
app.service('myService', function ($cacheFactory) {
// This is all you can do with $cacheFactory
$cacheFactory('myNewCache', { capacity: 1000 }); // This cache can hold 1000 items
});// Smarter caching with $angularCacheFactory
app.service('myService', function ($angularCacheFactory) {
$angularCacheFactory('myNewCache', {
capacity: 1000, // This cache can hold 1000 items,
maxAge: 90000, // Items added to this cache expire after 15 minutes
aggressiveDelete: true, // Items will be actively deleted when they expire
cacheFlushInterval: 3600000, // This cache will clear itself every hour,
storageMode: 'localStorage' // This cache will sync itself with localStorage
});
});Configure the cache to sync itself with localStorage or sessionStorage. The cache will re-initialize itself from localStorage and sessionStorage on page refresh.
$angularCacheFactory('newCache', { storageMode: 'localStorage' });When storageMode is set to "localStorage" or "sessionStorage" angular-cache will default to using the global localStorage and sessionStorage objects. The angular-cache localStorageImpl and sessionStorageImpl configuration parameters allow you to tell angular-cache which implementation of localStorage or sessionStorage to use. This is useful when you don't want to override the global storage objects or when using angular-cache in a browser that doesn't support localStorage or sessionStorage.
$angularCacheFactory('newCache', { localStorageImpl: myLocalStorageImplementation, storageMode: 'localStorage' });
$angularCacheFactory('otherCache', { localStorageImpl: mySessionStorageImplementation, storageMode: 'sessionStorage' });Note: If angular-cache doesn't detect a global localStorage or sessionStorage and you don't provide a polyfill, then that feature will be disabled. It is up to the developer to provide a polyfill for browsers that don't support localStorage and sessionStorage. Any implementation of localStorage and sessionStorage provided to angular-cache must implement at least the setItem, getItem, and removeItem methods.
Set a default maximum lifetime on all items added to the cache. They will be removed aggressively or passively depending on the value of aggressiveDelete (see below). Can be configured on a per-item basis for greater specificity.
$angularCacheFactory('newCache', { maxAge: 36000 });If true and maxAge is set, then items will be actively deleted right when they expire, otherwise items won't be deleted until they are requested but it is discovered that they have expired and are deleted, resulting in a miss. Can be configured on a per-item basis for greater specificity.
$angularCacheFactory('newCache', {
maxAge: 36000,
aggressiveDelete: true
});Set the cache to periodically clear itself.
$angularCacheFactory('newCache', { cacheFlushInterval: 57908 });Return the set of keys associated with all current caches owned by $angularCacheFactory.
$angularCacheFactory.keySet();Return the set of keys associated with all current items in someCache.
$angularCacheFactory.get('someCache').keySet();Return an array of the keys associated with all current caches owned by $angularCacheFactory.
$angularCacheFactory.keys();Return an array of the keys associated with all current items in someCache.
$angularCacheFactory.get('someCache').keys();Dynamically configure a cache.
$angularCacheFactory.get('someCache').setOptions({ capacity: 4500 });| Version | Branch | Build status | Test Coverage |
|---|---|---|---|
| 1.0.0-rc.1 | master | ![]() |
Test Coverage |
| 1.0.0-rc.1 | develop | ![]() |
|
| 1.0.0-rc.1 | all | ![]() |
| Type | From drone.io | From raw.github.com | Size |
|---|---|---|---|
| Production | angular-cache-1.0.0-rc.1.min.js | angular-cache-1.0.0-rc.1.min.js | 3.3 KB |
| Development | angular-cache-1.0.0-rc.1.js | angular-cache-1.0.0-rc.1.js | 28.7 KB |
bower install angular-cacheInclude src/angular-cache.js on your web page after angular.js.
Get angular-cache from the Download section and include it on your web page after angular.js.
- 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.js.
angular.module('myApp', ['angular-cache']);See angular-cache
app.service('myService', 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, // This cache can only hold 10 items.
maxAge: 90000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 600000, // This cache will clear itself every hour.
aggressiveDelete: true, // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage', // This cache will sync itself with `localStorage`.
localStorageImpl: myAwesomeLSImple // This cache will use a custom implementation of localStorage.
});
});app.service('myService', 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, ... }
});app.service('myOtherService', 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'); // undefinedSee 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 secondsSee AngularCache#put
myCache.put('someItem', { name: 'John Doe' });
myCache.remove('someItem');
myCache.get('someItem'); // undefinedmyCache.put('someItem', { name: 'John Doe' });
myCache.put('someOtherItem', { name: 'Sally Jean' });
myCache.removeAll();
myCache.get('someItem'); // undefined
myCache.get('someOtherItem'); // undefinedmyCache.destroy();
myCache.get('someItem'); // Will throw an error - Don't try to use a cache after destroying it!
$angularCacheFactory.get('myCache'); // undefinedmyCache.info(); // { id: 'myCache', size: 13 }- Added localStorage feature #26, #29
- Fixed #25
- Added a changelog #13
- Added documentation for installing with bower
- Added ability to set option
aggressiveDeletewhen creating cache and when adding items - Cleaned up README.md
- Switched the demo to use Bootstrap 3
- Added CONTRIBUTING.md #22
- Cleaned up meta data in bower.json and package.json
- Added .jshintrc
- Cleaned up the docs a bit
bower.jsonnow usessrc/angular-cache.jsinstead of the versioned output files #21- From now on the tags for the project will be named using semver
- Added
AngularCache.setOptions(), the ability to dynamically change the configuration of a cache #20 - Added
AngularCache.keys(), which returns an array of the keys in a cache #19 - Added
AngularCache.keySet(), which returns a hash of the keys in a cache #19
- Added
angular-cacheto bower registry #7 - Created a working demo #9 #17
- Fixed the size not being reset to 0 when the cache clears itself #14 #16
- Added
$angularCacheFactory.keys(), which returns an array of the keys (the names of the caches) in $angularCacheFactory #18 - Added
$angularCacheFactory.keySet(), which returns a hash of the keys (the names of the caches) in $angularCacheFactory #18
- Got the project building on TravisCI
- Renamed the project to
angular-cache#5
- Added a roadmap to README.md #4
- Clarify usage documentation #3
- Wrote unit tests #2
- Added Grunt build tasks #1
- 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
developand 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 startorgrunt 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
developbranch - 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.


