NgxCachrService

The core service responsible for managing the cache layers (Memory and Storage). It handles the execution of caching strategies and coordination between drivers.

Generally, you shouldn't need to inject this service directly. Use the cachedResource function instead for a higher-level, reactive API.

Constructor

new NgxCachrService()

Methods

(async) get(options, updateStateopt) → {Promise.<(T|undefined)>}

Resolves a resource using the configured caching strategy.

Parameters:
NameTypeAttributesDescription
optionsCachedResourceOptions.<T>

Options defining the resource, key, and strategy.

updateStatefunction<optional>

Optional callback to receive state updates (loading, success, error, revalidating).

Returns:

A promise that resolves to the data (if strategy allows immediate return) or undefined.

Type: 
Promise.<(T|undefined)>
Example
const data = await service.get({
  key: 'user:1',
  loader: () => fetch('/api/user/1').then(r => r.json()),
  strategy: 'swr'
});

(async) get(options, updateStateopt) → {Promise.<(T|undefined)>}

Resolves a resource using the configured caching strategy.

Parameters:
NameTypeAttributesDescription
optionsCachedResourceOptions.<T>

Options defining the resource, key, and strategy.

updateStatefunction<optional>

Optional callback to receive state updates (loading, success, error, revalidating).

Returns:

A promise that resolves to the data (if strategy allows immediate return) or undefined.

Type: 
Promise.<(T|undefined)>
Example
const data = await service.get({
  key: 'user:1',
  loader: () => fetch('/api/user/1').then(r => r.json()),
  strategy: 'swr'
});

getDebugSnapshot()

Returns a debug snapshot of the current cache state.

getDebugSnapshot() → {CacheSnapshot}

Returns a debug snapshot of the current cache state.

Returns:
Type: 
CacheSnapshot

(async) invalidate(key) → {Promise.<void>}

Invalidates a cache entry by removing it from both memory and storage.

Parameters:
NameTypeDescription
keyCacheKey

The key to invalidate.

Returns:
Type: 
Promise.<void>

(async) invalidate(key) → {Promise.<void>}

Invalidates a cache entry by removing it from both memory and storage.

Parameters:
NameTypeDescription
keyCacheKey

The key to invalidate.

Returns:
Type: 
Promise.<void>

(async) set(key, data, ttlopt) → {Promise.<void>}

Manually sets a cache entry.

Parameters:
NameTypeAttributesDescription
keyCacheKey

The key to set.

dataT

The data to store.

ttlnumber<optional>

Optional time-to-live in ms.

Returns:
Type: 
Promise.<void>

(async) set(key, data, ttlopt) → {Promise.<void>}

Manually sets a cache entry.

Parameters:
NameTypeAttributesDescription
keyCacheKey

The key to set.

dataT

The data to store.

ttlnumber<optional>

Optional time-to-live in ms.

Returns:
Type: 
Promise.<void>