View source Improve this doc

$cacheFactory

service in module ng

Description

Factory that constructs cache objects and gives access to them.

  1. var cache = $cacheFactory('cacheId');
  2. expect($cacheFactory.get('cacheId')).toBe(cache);
  3. expect($cacheFactory.get('noSuchCacheId')).not.toBeDefined();
  4.  
  5. cache.put("key", "value");
  6. cache.put("another key", "another value");
  7.  
  8. // We've specified no options on creation
  9. expect(cache.info()).toEqual({id: 'cacheId', size: 2});
  10.  

Usage

  1. $cacheFactory(cacheId[, options]);

Parameters

ParamTypeDetails
cacheIdstring Name or id of the newly created cache.
options (optional) object Options object that specifies the cache behavior. Properties: - {number=} capacity — turns the cache into LRU cache.

Returns

object Newly created cache object with the following set of methods: - {object} info() — Returns id, size, and options of cache. - {{*}} put({string} key, {*} value) — Puts a new key-value pair into the cache and returns it. - {{*}} get({string} key) — Returns cached value for key or undefined for cache miss. - {void} remove({string} key) — Removes a key-value pair from the cache. - {void} removeAll() — Removes all cached values. - {void} destroy() — Removes references to this cache from $cacheFactory.

Methods

  • get(cacheId)

Get access to a cache object by the cacheId used when it was created.

Parameters

ParamTypeDetailscacheIdstring

Name or id of a cache to access.

Returns

object

Cache object identified by the cacheId or undefined if no such cache.

  • info()

Get information about all the of the caches that have been created

Returns

Object

  • key-value map of cacheId to the result of calling cache#info