A very useful replacement for Angular's $cacheFactory.
versions of angular-cache below 4.0.0 have been deprecated, see the breaking changes in 4.0.0
- Quick Start
- The Basics
- Working with a cache
- Configuration Options
- Using angular-cache with $http
- Using angular-cache with localStorage
- API Reference
bower install --save angular-cache
or npm install --save angular-cache
.
angular.module('myApp', ['angular-cache'])
.config(function (CacheFactoryProvider) {
angular.extend(CacheFactoryProvider.defaults, { maxAge: 15 * 60 * 1000 });
})
.service('BookService', function (CacheFactory, $http) {
if (!CacheFactory.get('bookCache')) {
// or CacheFactory('bookCache', { ... });
CacheFactory.createCache('bookCache', {
deleteOnExpire: 'aggressive',
recycleFreq: 60000
});
}
var bookCache = CacheFactory.get('bookCache');
return {
findBookById: function (id) {
return $http.get('/api/books/' + id, { cache: bookCache });
}
};
});
Feature | $cacheFactory | angular-cache |
---|---|---|
core implementation usable outside of Angular | no | yes |
key/value store | yes | yes |
in-memory support | yes | yes |
localStorage support | no | yes |
sessionStorage support | no | yes |
custom storage support | no | yes |
can set maximum capacity | yes | yes |
LRU support | yes | yes |
support for time-based expiration of items | no | yes |
remove all expired items | n/a | yes |
"touch" an item | n/a | yes |
execute callback when an item expires | n/a | yes |
get info about cache | yes | yes |
get info about single item | no | yes |
get item by key | yes | yes |
remove item by key | yes | yes |
remove all items | yes | yes |
get keys of all caches | no | yes |
clear all caches | no | yes |
disable/enable a cache | no | yes |
"touch" all items in all caches | n/a | yes |
remove expired items from all caches | n/a | yes |
get all keys in a cache | no | yes |
clear a cache on a given interval | no | yes |
First, inject CacheFactory
then create a cache. Let's go:
app.service('myService', function (CacheFactory) {
var profileCache;
// Check to make sure the cache doesn't already exist
if (!CacheFactory.get('profileCache')) {
profileCache = CacheFactory('profileCache');
}
});
Let's add some items to the cache:
profileCache.put('/profiles/34', {
name: 'John',
skills: ['programming', 'piano']
});
profileCache.put('/profiles/22', {
name: 'Sally',
skills: ['marketing', 'climbing', 'painting']
});
Right now, these items will stay in the cache until a page refresh.
Let's have items which are added to profileCache
expire after an hour:
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour
});
Perfect. Say we also want the items removed from the cache when they expire:
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour,
deleteOnExpire: 'aggressive'
});
Let's say that when the items do expire, we want to refresh them with new values:
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour,
deleteOnExpire: 'aggressive',
onExpire: function (key, value) {
$http.get(key).success(function (data) {
profileCache.put(key, data);
});
}
});
Or say we want all of our caches to use that configuration as their default:
angular.module('app', ['angular-cache']).config(function (CacheFactoryProvider) {
angular.extend(CacheFactoryProvider.defaults, {
maxAge: 3600000,
deleteOnExpire: 'aggressive',
onExpire: function (key, value) {
var _this = this; // "this" is the cache in which the item expired
angular.injector(['ng']).get('$http').get(key).success(function (data) {
_this.put(key, data);
});
}
});
});
We can retrieve items from a cache like so:
var profile = profileCache.get('/profiles/34');
profile.name; // 'John'
And get information about items in the cache:
var info = profileCache.info('/profiles/34');
info.isExpired; // false
// etc.
and information about the cache itself:
var info = profileCache.info();
info.size; // 2
info.maxAge; // 3600000
info.deleteOnExpire; // 'aggressive'
// etc.
Items are easily removed, and we can destroy our cache when we're done with it:
profileCache.remove('/profiles/34');
profileCache.get('/profiles/34'); // undefined
profileCache.destroy();
CacheFactory.get('profileCache'); // undefined
These options apply to:
CacheFactory(cacheId[, options)
CacheFactory.createCache(cacheId[, options])
Cache#setOptions(options[, strict])
Cache#setMaxAge(maxAge)
,Cache#setOnExpire(onExpire)
, etc.
If set, remove all items from a cache on an interval after the given number of milliseconds. Default: null
.
Maximum number of items a cache can hold. Adding more items than the capacity will cause the cache to operate like an LRU cache, removing the least recently used items to stay under capacity. Default: Number.MAX_VALUE
.
Determines the behavior of a cache when an item expires. Default: none
.
Possible values:
none
- Cache will do nothing when an item expires.passive
- Cache will do nothing when an item expires. Expired items will remain in the cache until requested, at which point they are removed, andundefined
is returned.aggressive
- Cache will remove expired items as soon as they are discovered.
Determines whether a cache is disabled. Default: false
.
A callback function to be executed whenever an expired item is removed from a cache when the cache is in passive
or aggressive
mode. Will be passed the key
and value
of the expired item.
Will be passed a third done
argument if the cache is in passive
mode. This allows you to synchronously access the key
and value
of the expired item when you make the Cache#get(key[, options])
call that is the reason the expired item is being removed in the first place. Default: null
.
The number of milliseconds until a newly inserted item expires. Default: Number.MAX_VALUE
.
Determines how often a cache will scan for expired items when in aggressive
mode. Default: 1000
(milliseconds).
Provide a custom storage medium, e.g. a polyfill for localStorage
. Default: null
.
Must implement:
setItem
- Same API aslocalStorage.setItem(key, value)
getItem
- Same API aslocalStorage.getItem(key)
removeItem
- Same API aslocalStorage.removeItem(key)
Determines the storage medium used by a cache. Default: memory
.
Possible values:
memory
- Cache will hold data in memory. Data is cleared when the page is refreshed.localStorage
- Cache will hold data inlocalStorage
if available. Data is not cleared when the page is refreshed.sessionStorage
- Cache will hold data insessionStorage
if available. Data is not cleared when the page is refreshed.
Determines the namespace of a cache when storageMode
is set to localStorage
or sessionStorage
. Make it a shorter string to save space. Default: angular-cache.caches.
.
If inserting a promise into a cache, also insert the rejection value if the promise rejects. Default: false
.
If inserting a promise into a cache, also insert the resolved value if the promise resolves. Default: false
.
Note: The downside of letting $http
handle caching for you is that it caches the responses (in string form) to your requests–not the JavaScript Object parsed from the response body. This means you can't interact with the data in the cache used by $http
. See below for how to handle the caching yourself, which gives you more control and the ability to interact with the cache (use it as a data store).
Configure $http
to use a cache created by CacheFactory
by default:
app.run(function ($http, CacheFactory) {
$http.defaults.cache = CacheFactory('defaultCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
});
app.service('MyService', function ($http, $q) {
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
$http.get('api/data/' + id, {
cache: true
}).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
deferred.resolve(data);
});
return deferred.promise;
}
};
});
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
Tell $http to use a cache created by CacheFactory for a specific request:
app.service('MyService', function ($q, $http, CacheFactory) {
CacheFactory('dataCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
$http.get('api/data/' + id, {
cache: CacheFactory.get('dataCache')
}).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
deferred.resolve(data);
});
return deferred.promise;
}
};
});
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
Do your own caching while using the $http service:
app.service('MyService', function ($q, $http, CacheFactory) {
CacheFactory('dataCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
var dataCache = CacheFactory.get('dataCache');
// Now that control of inserting/removing from the cache is in our hands,
// we can interact with the data in "dataCache" outside of this context,
// e.g. Modify the data after it has been returned from the server and
// save those modifications to the cache.
if (dataCache.get(id)) {
deferred.resolve(dataCache.get(id));
} else {
$http.get('api/data/' + id).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
dataCache.put(id, data);
deferred.resolve(data);
});
}
return deferred.promise;
}
};
});
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
app.service('myService', function (CacheFactory) {
// This cache will sync itself with localStorage if it exists, otherwise it won't. Every time the
// browser loads this app, this cache will attempt to initialize itself with any data it had
// already saved to localStorage (or sessionStorage if you used that).
var myAwesomeCache = CacheFactory('myAwesomeCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will use `localStorage`.
});
});
Using angular-cache in browsers that DON'T support localStorage:
Option 1 - Do nothing (the cache will just store data in memory)
Option 2 - Create/use a polyfill that provides the global localStorage
and sessionStorage
objects. angular-cache will attempt to use these if it finds them.
Option 3 - Tell angular-cache exactly which polyfill to use (also useful if you just want to use your own implementation/wrapper for localStorage):
app.service('myService', function (CacheFactory) {
var localStoragePolyfill = {
getItem: function (key) { ... },
setItem: function (key, value) { ... },
removeItem: function (key) { ... }
};
// Always use the polyfill
var myAwesomeCache = CacheFactory('myAwesomeCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage', // This cache will use `localStorage`.
storageImpl: localStoragePolyfill // angular-cache will use this polyfill instead of looking for localStorage
});
// Conditionally use the polyfill
var options = {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will use `localStorage`.
};
if (!window.localStorage) {
options.storageImpl = localStoragePolyfill;
}
var myAwesomeCache = CacheFactory('myAwesomeCache', options);
});
Documentation on the interface that must be implemented by any storageImpl polyfill used by angular-cache can be found on the W3C Recommendation page for webstorage. The interface itself looks like:
interface Storage {
readonly attribute unsigned long length;
DOMString? key(unsigned long index);
getter DOMString getItem(DOMString key);
setter creator void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};
angular-cache cares only about these three methods:
setItem
getItem
removeItem
One developer suggested using store.js–a wrapper and polyfill for localStorage. However, store.js has its own API that doesn't match that of the webstorage spec, so if you want to use store.js or any other 3rd-party polyfill then you'll need to create a wrapper for it if it doesn't have the same API as localStorage . For example:
var storeJsToStandard {
getItem: store.get,
setItem: store.set,
removeItem: store.remove
};
CacheFactory('myNewCache', {
storageMode: 'localStorage',
storageImpl: storeJsToStandard
});
Create a cache. Cache must not already exist. cacheId
must be a string. options
is an optional argument and must be an object. Any options you pass here will override any default options.
var cache = CacheFactory('cache');
var cache2 = CacheFactory.createCache('cache2');
var cache3 = CacheFactory('cache', { maxAge: 900000 });
var cache4 = CacheFactory('cache'); // Error "cache already exists!"
Return the cache with the given cacheId
.
Return an object of key-value pairs, the keys being cache ids and the values being the result of .info()
being called on each cache.
Return the ids of all registered caches as an object.
Return the ids of all registered caches as an array.
Destroy the cache with the given cacheId
.
Destroy all registered caches.
Remove all data from all registered caches.
Enable all registered caches.
Disable all registered caches.
Call .touch()
on all registered caches.
Call .removeExpired()
on all registered caches. Returns a hash of any expired items, keyed by cache id.
Return the item with the given key
. options
, if provided, must be an object.
If the cache is in passive
mode, then options.onExpire
can be a function that will be called with the key
and value
of the requested item if the requested item is expired, with the get
call itself returning undefined.
Insert the item with the given key
and value
into the cache. options
, if provided, must be an object.
If inserting a promise, options.storeOnReject
determines whether to insert the rejection value if the promise rejects (overriding the default storeOnReject
setting for the cache).
If inserting a promise, options.storeOnResolve
determines whether to insert the resolved value if the promise resolves (overriding the default storeOnResolve
setting for the cache).
Remove and return the item with the given key
, if it is in the cache.
Remove all items in the cache.
Remove and return all expired items in the cache.
Completely destroy this cache and its data.
Cache#info()
returns an object containing information about the cache.
Cache#info(key)
returns an object containing information about the item with the given key
, if the item is in the cache.
Return the keys of all items in the cache as an object.
Return the keys of all items in the cache as an array.
Enable the cache.
Disable the cache.
Return all values in the cache.
Cache#touch()
will "touch" all items in the cache.
Cache#touch(key)
will "touch" the item with the given key
.
Set the cacheFlushInterval
for the cache.
Set the capacity
for the cache. Setting this lower than the current item count will result in those items being removed.
Set the deleteOnExpire
for the cache.
Set the maxAge
for the cache.
Set the onExpire
for the cache.
Set the recycleFreq
for the cache.
Set the storageMode
for the cache. This will move data from the current storage medium to the new one.
Set multiple options for the cache at a time. Setting strict
to true
will reset options for the cache that are not specifically set in the options
hash to CacheFactoryProvider.defaults
.
Copyright (C) 2013-2016 angular-cache project authors
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.