Reference API documentation for angular-cache.
Provider for $angularCacheFactory
. Injectable into an Angular module's config()
method call.
app.module('app', ['jmdobry.angular-cache'])
.config(function ($angularCacheFactoryProvider) {
// ...
});
Set the default configuration for all caches created by $angularCacheFactory
.
Type: object
Description: Configuration object for global cache defaults. See Configuration Options.
app.module('app', ['jmdobry.angular-cache'])
.config(function ($angularCacheFactoryProvider) {
$angularCacheFactoryProvider.setCacheDefaults({
maxAge: 3600000,
deleteOnExpire: 'aggressive'
});
})
.run(function ($angularCacheFactory) {
var info = $angularCacheFactory.info();
console.log(info.cacheDefaults); // output below
// {
// capacity: Number.MAX_VALUE,
// maxAge: 3600000,
// deleteOnExpire: 'aggressive',
// onExpire: null,
// cacheFlushInterval: null,
// storageMode: 'none',
// storageImpl: null,
// recycleFreq: 1000,
// disabled: false,
// verifyIntegrity: false
// }
var newCache = $angularCacheFactory('newCache');
newCache.info().maxAge; // 3600000
newCache.info().deleteOnExpire; // "aggressive"
});
Angular Factory Service. Produces instances of AngularCache
.
Type: string
Required: Yes
Description: The name of the new cache. Must be unique.
Type: object
Required: No
Description: The configuration options for the new cache. See Configuration Options;
angular.module('app').controller('myCtrl', function ($angularCacheFactory) {
var newCache = $angularCacheFactory('newCache', { options... });
});
Return the cache with the specified cacheId.
Type: string
Required: Yes
Description: The cacheId of the cache to retrieve.
var someCache = $angularCacheFactory.get('someCache');
Return an object containing information about $angularCacheFactory
and all caches in $angularCacheFactory
.
$angularCacheFactory.info(); // { info...}
Clear the contents of every cache in $angularCacheFactory
.
$angularCacheFactory.clearAll();
Destroy all caches in $angularCacheFactory
.
$angularCacheFactory.removeAll();
Return the set of cacheIds of all caches in $angularCacheFactory
.
$angularCacheFactory.keySet();
Return an array of the cacheIds of all caches in $angularCacheFactory
.
$angularCacheFactory.keys();
Disable all caches. No data will be lost.
$angularCacheFactory.disableAll();
Enable all caches. Caches will resume with full functionality.
$angularCacheFactory.enableAll();
Object produced by invocations of $angularCacheFactory(cacheId, options)
.
var newCache = $angularCacheFactory('newCache');
newCache; // instance of AngularCache
AngularCache#setOptions(options[, strict])
Dynamically set the configuration for the cache. See Configuration Options.
Type: object
Description: The configuration options for the cache. See Configuration Options.
Type: boolean
Required: No
Description: If true, the cache's configuration will be reset to default before applying the new configuration.
$angularCacheFactory.get('someCache').setOptions({ maxAge: 900000 });
$angularCacheFactory.get('someCache').setOptions({ verifyIntegrity: false }, true);
AngularCache#put(key, value[, options])
Add a key-value pair to the cache.
Type: string
Required: Yes
Description: The key of the item to be added to the cache.
Type: *
Required: Yes
Description: The item to be added to the cache.
Type: object
Required: No
Description: The configuration options for the item to be added.
someCache.put('key', 'value');
someCache.put('ageLimit', 55);
someCache.put('things', { stuff: 'lots of stuff' });
someCache.put('isIt', true);
AngularCache#get(key[, options])
Retrieve the item from the cache with the specified key.
Type: string|array
Description: The key of the item to retrieve or an array of the keys of items to retrieve.
Type: object
Description: Configuration options for this method call. Possible properties of options
parameter:
onExpire
- function
- Function to be called if the requested item has expired.someCache.get('key'); // 'value'
someCache.get('ageLimit'); // 55
someCache.get('things'); // { stuff: 'lots of stuff' }
someCache.get('isIt'); // true
someCache.get(['key', 'ageLimit']); // ['value', 55]
var thing = someCache.get('thingThatExpired', {
onExpire: function (key, value) {
$http.get(key).then(function (thing) {
someCache.put(key, thing);
someCache.get('thingThatExpired'); // { name: 'A thing!' }
});
});
thing; // undefined
AngularCache#remove(key[, options])
Remove the item with the specified key from the cache.
Type: object
Description: Configuration options for this method call. verifyIntegrity: true|false
is the only option available for this method.
someCache.remove('someKey');
AngularCache#removeExpired([options])
Remove all expired items from the cache and return an object (or array) of the removed items.
Type: object
Description: Configuration options for this method call. verifyIntegrity: true|false
is available, as well as asArray: true|false
, which will cause the method to return an array instead of an object.
someCache.removeExpired(options);
Clear the cache.
someCache.removeAll();
Completely destroy the cache.
someCache.destroy();
Return an object containing information about the item with the specified key, or if no key is specified, return information about the cache itself.
Type: string
Required: No
Description: The key of the item about which to retrieve information.
someCache.info('someKey'); // { // info about 'someKey'... }
someCache.info(); // { // info about 'someCache'... }
Return the set of keys of all items in the cache.
someCache.keySet();
Return an array of the keys of all items in the cache.
someCache.keys();