ResourceCacheService

ResourceCacheService

new ResourceCacheService(name, pkAttr, optionsopt)

Factory that gives a cache class whose instances can be used for caching resources. Instances of this cache class
are capable of invalidating related caches, updating resource list instances when getting data from detail calls
and respecting the dataAttr of resource instances.

Parameters:
Name Type Attributes Description
name String

Name of the cache

pkAttr String

Name of the primary key attribute on cached instances

options Object <optional>

Additional configuration

Properties
Name Type Description
urlAttr String | null

Name of the attribute to get the URL of the objects (default: null)

dataAttr String | null

Name of the attribute to get the actual data from (default: null)

wrapObjectsInDataAttr Boolean

When object cache entries are created from list results, use the dataAttr to wrap the data (default: false)

dependent Array.<String>

List of dependent cache names (default: [])

ttl int

Time to live for cache entries in seconds (default: 3600)

Source:
Examples
// Directly using `ResourceCacheService`
inject(function (ResourceCacheService) {
   var
       exampleValue = {pk: 123, attr1: 1, attr2: 2},
       cacheInstance = new ResourceCacheService('MyExampleResourceCache', 'pk');

   cacheInstance.put('example-1', exampleValue, false);
   expect(cacheInstance.get('example-1', false)).toEqual(exampleValue);
});
// Using `ResourceCacheService` for `$http`
inject(function (ResourceCacheService, $http) {
   var
       cacheInstance = new ResourceCacheService('MyExampleResourceCache', 'pk'),
       httpInstance = $http({cache: cacheInstance.withoutDataAttr});

   httpInstance.get('http://example.com/api/endpoint-1/');
});

Members

withDataAttr :HttpCacheInstance

Cache interface to put entries using dataAttr on the cache.

Type:
Source:

withDataAttrNoTtl :HttpCacheInstance

Cache interface to put entries using dataAttr on the cache and ignoring TTL.

Type:
Source:

withoutDataAttr :HttpCacheInstance

Cache interface to put entries without using dataAttr on the cache.

Type:
Source:

withoutDataAttrNoTtl :HttpCacheInstance

Cache interface to put entries without using dataAttr on the cache and ignoring TTL.

Type:
Source:

Methods

(static) get(key) → {ResourceCacheService}

Gets the cache with the given name, or null.

Parameters:
Name Type Description
key String

Cache name

Source:
Returns:

Cache instance with the given name

Type
ResourceCacheService

(static) info() → {Object}

Gets the cache information for all managed caches as mapping of cacheId to the result
of the info method on the cache.

Source:
Returns:

Information object for all caches

Type
Object

(static) removeAll()

Calls the removeAll method on all managed caches.

Source:

destroy()

Destroys the cache object.

Source:

findByPk(pkValue) → {Array.<Object>}

Gets all items from cache that match the given PK value. If there is no item on the
cache that matches, this method returns an empty array. Note that the cache TTL is ignored.

Parameters:
Name Type Description
pkValue String | int

PK value to search for

Source:
Returns:

Search results data

Type
Array.<Object>

firstByPk(pkValue) → {Object|undefined}

Gets the first item from cache that matches the given PK value. If there is no item on the
cache that matches, this method returns undefined. Note that the cache TTL is ignored.

Parameters:
Name Type Description
pkValue String | int

PK value to search for

Source:
Returns:

Search result data

Type
Object | undefined

get(key, useCacheTtlopt) → {*}

Gets the entry with the given key from the cache, or undefined.

Parameters:
Name Type Attributes Description
key String

Cache entry key

useCacheTtl Boolean <optional>

If true this method will return undefined when the TTL of the entry is outreached (default: true)

Source:
Returns:

Cache entry

Type
*

info() → {ResourceCacheServiceMeta}

Retrieve information regarding the cache.

Source:
Returns:

Information about the cache instance

Type
ResourceCacheServiceMeta

insert(key, value, useDataAttr, refreshopt)

Creates a cache entry for the given value and puts it on the cache.

Parameters:
Name Type Attributes Description
key String

Cache entry key

value *

Cache entry value

useDataAttr Boolean

Use the dataAttr to get actual cache entry value

refresh Boolean <optional>

Refresh the existing cache entries by using the new value

Source:

put(key, value, useDataAttr)

Puts the given entry with the given key on the cache.

Parameters:
Name Type Description
key String

Cache entry key

value *

Cache entry value

useDataAttr Boolean

Use the dataAttr to get actual cache entry value

Source:

refresh(value)

Refreshes the cache entries with the new value or values. The existing objects in the cache
are matched by the pkAttr value, and additionally by the urlAttr, if available.

Parameters:
Name Type Description
value Object | Array.<Object>

Instance or list of instances used to refresh data on the cache

Source:

remove(key)

Removes the entry with the given key from the cache.

Parameters:
Name Type Description
key String

Cache entry key

Source:

removeAll()

Removes all entries from the cache.

Source:

removeAllDependent()

Removes all entries of the dependent caches, including the dependent caches of the
dependent caches (and so on ...).

Source:

removeAllLists()

Removes all list entries from the cache.

Source:

removeAllObjects()

Removes all list entries from the cache.

Source:

removeAllRaw()

Removes all raw entries from the cache.

Source: