Improve this Doc  View Source

$cacheFactory.Cache

  1. - type in module ng

A cache object used to store and retrieve data, primarily used by $http and the script directive to cache templates and other data.

angular.module('superCache')
  .factory('superCache', ['$cacheFactory', function($cacheFactory) {
    return $cacheFactory('super-cache');
  }]);

Example test:

it('should behave like a cache', inject(function(superCache) {
  superCache.put('key', 'value');
  superCache.put('another key', 'another value');

  expect(superCache.info()).toEqual({
    id: 'super-cache',
    size: 2
  });

  superCache.remove('another key');
  expect(superCache.get('another key')).toBeUndefined();

  superCache.removeAll();
  expect(superCache.info()).toEqual({
    id: 'super-cache',
    size: 0
  });
}));

Methods

  • put(key, value);

    Inserts a named entry into the Cache object to be retrieved later, and incrementing the size of the cache if the key was not already present in the cache. If behaving like an LRU cache, it will also remove stale entries from the set.

    It will not insert undefined values into the cache.

    Parameters

    Param Type Details
    key string

    the key under which the cached data is stored.

    value *

    the value to store alongside the key. If it is undefined, the key will not be stored.

    Returns

    *

    the value stored.

  • get(key);

    Retrieves named data stored in the Cache object.

    Parameters

    Param Type Details
    key string

    the key of the data to be retrieved

    Returns

    *

    the value stored.

  • remove(key);

    Removes an entry from the Cache object.

    Parameters

    Param Type Details
    key string

    the key of the entry to be removed

  • removeAll();

    Clears the cache object of any entries.

  • destroy();

    Destroys the Cache object entirely, removing it from the $cacheFactory set.

  • info();

    Retrieve information regarding a particular Cache.

    Returns

    object

    an object with the following properties:

    • id: the id of the cache instance
    • size: the number of entries kept in the cache instance
    • ...: any additional properties from the options object when creating the cache.