The docs for angular-cache 2.x.x are being deprecated. Please consider upgrading to 3.x.x
Fork me on GitHub

API

Reference API documentation for angular-cache.

Description

Provider for $angularCacheFactory. Injectable into an Angular module's config() method call.

Usage

app.module('app', ['jmdobry.angular-cache'])
    .config(function ($angularCacheFactoryProvider) {
        // ...
    });

Description

Set the default configuration for all caches created by $angularCacheFactory.

Parameters

options

Type: object

Description: Configuration object for global cache defaults. See Configuration Options.

Usage

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"
    });

Description

Angular Factory Service. Produces instances of AngularCache.

Parameters

cacheId

Type: string

Required: Yes

Description: The name of the new cache. Must be unique.

options

Type: object

Required: No

Description: The configuration options for the new cache. See Configuration Options;

Usage

angular.module('app').controller('myCtrl', function ($angularCacheFactory) {
    var newCache = $angularCacheFactory('newCache', { options... });
});

Description

Return the cache with the specified cacheId.

Parameters

cacheId

Type: string

Required: Yes

Description: The cacheId of the cache to retrieve.

Usage

var someCache = $angularCacheFactory.get('someCache');

Description

Return an object containing information about $angularCacheFactory and all caches in $angularCacheFactory.

Usage

$angularCacheFactory.info(); // { info...}

Description

Clear the contents of every cache in $angularCacheFactory.

Usage

$angularCacheFactory.clearAll();

Description

Destroy all caches in $angularCacheFactory.

Usage

$angularCacheFactory.removeAll();

Description

Return the set of cacheIds of all caches in $angularCacheFactory.

Usage

$angularCacheFactory.keySet();

Description

Return an array of the cacheIds of all caches in $angularCacheFactory.

Usage

$angularCacheFactory.keys();

.disableAll()

Description

Disable all caches. No data will be lost.

Usage

$angularCacheFactory.disableAll();

.enableAll()

Description

Enable all caches. Caches will resume with full functionality.

Usage

$angularCacheFactory.enableAll();

Description

Object produced by invocations of $angularCacheFactory(cacheId, options).

Usage

var newCache = $angularCacheFactory('newCache');

newCache; // instance of AngularCache

Signature

AngularCache#setOptions(options[, strict])

Description

Dynamically set the configuration for the cache. See Configuration Options.

Parameters

options

Type: object

Description: The configuration options for the cache. See Configuration Options.

strict

Type: boolean

Required: No

Description: If true, the cache's configuration will be reset to default before applying the new configuration.

Usage

$angularCacheFactory.get('someCache').setOptions({ maxAge: 900000 });

$angularCacheFactory.get('someCache').setOptions({ verifyIntegrity: false }, true);

Signature

AngularCache#put(key, value[, options])

Description

Add a key-value pair to the cache.

Parameters

key

Type: string

Required: Yes

Description: The key of the item to be added to the cache.

value

Type: *

Required: Yes

Description: The item to be added to the cache.

options

Type: object

Required: No

Description: The configuration options for the item to be added.

Usage

someCache.put('key', 'value');

someCache.put('ageLimit', 55);

someCache.put('things', { stuff: 'lots of stuff' });

someCache.put('isIt', true);

Signature

AngularCache#get(key[, options])

Description

Retrieve the item from the cache with the specified key.

Parameters

key

Type: string|array

Description: The key of the item to retrieve or an array of the keys of items to retrieve.

options

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.

Usage

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

Signature

AngularCache#remove(key[, options])

Description

Remove the item with the specified key from the cache.

Parameters

options

Type: object

Description: Configuration options for this method call. verifyIntegrity: true|false is the only option available for this method.

Usage

someCache.remove('someKey');

Signature

AngularCache#removeExpired([options])

Description

Remove all expired items from the cache and return an object (or array) of the removed items.

Parameters

options

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.

Usage

someCache.removeExpired(options);

Description

Clear the cache.

Usage

someCache.removeAll();

Description

Completely destroy the cache.

Usage

someCache.destroy();

Description

Return an object containing information about the item with the specified key, or if no key is specified, return information about the cache itself.

Parameters

options

Type: string

Required: No

Description: The key of the item about which to retrieve information.

Usage

someCache.info('someKey'); // { // info about 'someKey'... }

someCache.info(); // { // info about 'someCache'... }

.keySet()

Description

Return the set of keys of all items in the cache.

Usage

someCache.keySet();

.keys()

Description

Return an array of the keys of all items in the cache.

Usage

someCache.keys();