Skip to main content

Options interfaces

Two small option types control the behaviour of set() and cacheObservable().

ISetOptions

import { ISetOptions } from 'ng-ncached';

interface ISetOptions {
ttl?: number;
}

Passed as the last argument to set().

FieldTypeDefaultNotes
ttlnumber?undefinedTime-to-live in milliseconds. Omit for no expiration. 0 is treated as no TTL.

Example

this._cache.set(token, 'auth', 'token', { ttl: 60_000 });

When ttl is set, the entry is internally written with expiresAt = Date.now() + ttl. See TTL & expiration.


ICacheObservableOptions

import { ICacheObservableOptions } from 'ng-ncached';

interface ICacheObservableOptions<T = unknown> {
ttl?: number;
defaultValue?: T;
}

Passed as the second argument to cacheObservable().

FieldTypeDefaultNotes
ttlnumber?undefinedTime-to-live in milliseconds for the cached result. Omit for no expiration.
defaultValueT?noneEmitted if the source observable errors. Omit to let errors propagate.

defaultValue is only consulted on source error, not on cache miss. Cache misses always go to the network.

Example

this._cache.cacheObservable<IConfig>(
this._http.get<IConfig>('/api/config'),
{ ttl: 5 * 60_000, defaultValue: DEFAULTS },
'api', 'config',
);

See Caching observables.