Skip to content 

PromiseCache Class

Packages > @fluidframework/common-utils > PromiseCache

A specialized cache for async work, allowing you to safely cache the promised result of some async work without fear of running it multiple times or losing track of errors.

Signature:

export declare class PromiseCache<TKey, TResult> 

Constructors

List of constructors for this class
Constructor Modifiers Description
(constructor)({ expiry, removeOnError, }) Create the PromiseCache with the given options, with the following defaults:expiry: indefinite, removeOnError: true for all errors

Methods

List of methods on this class
Method Modifiers Description
add(key, asyncFn) Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.
addOrGet(key, asyncFn) Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.
addValue(key, value) Try to add the given value, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.
addValueOrGet(key, value) Try to add the given value, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.
get(key) Get the Promise for the given key, or undefined if it's not found. Extend expiry if applicable.
has(key) Check if there's anything cached at the given key
remove(key) Remove the Promise for the given key, returning true if it was found and removed

Constructors

PromiseCache.(constructor)

Create the PromiseCache with the given options, with the following defaults:

expiry: indefinite, removeOnError: true for all errors

Signature:

constructor({ expiry, removeOnError, }?: PromiseCacheOptions);

Parameters

List of parameters
Parameter Type Description
{ expiry, removeOnError, } PromiseCacheOptions

Methods

add

Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.

Signature:

add(key: TKey, asyncFn: () => Promise<TResult>): boolean;

Parameters

List of parameters
Parameter Type Description
key TKey key name where to store the async work
asyncFn () => Promise the async work to do and store, if not already in progress under the given key

Returns:

boolean

addOrGet

Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.

Signature:

addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;

Parameters

List of parameters
Parameter Type Description
key TKey key name where to store the async work
asyncFn () => Promise the async work to do and store, if not already in progress under the given key

Returns:

Promise<TResult>

addValue

Try to add the given value, without overwriting an existing cache entry at that key. Returns false if the cache already contained an entry at that key, and true otherwise.

Signature:

addValue(key: TKey, value: TResult): boolean;

Parameters

List of parameters
Parameter Type Description
key TKey key name where to store the value
value TResult value to store

Returns:

boolean

addValueOrGet

Try to add the given value, without overwriting an existing cache entry at that key. Returns a Promise for the added or existing async work being done at that key.

Signature:

addValueOrGet(key: TKey, value: TResult): Promise<TResult>;

Parameters

List of parameters
Parameter Type Description
key TKey key name where to store the async work
value TResult value to store

Returns:

Promise<TResult>

get

Get the Promise for the given key, or undefined if it’s not found. Extend expiry if applicable.

Signature:

get(key: TKey): Promise<TResult> | undefined;

Parameters

List of parameters
Parameter Type Description
key TKey

Returns:

Promise<TResult> | undefined

has

Check if there’s anything cached at the given key

Signature:

has(key: TKey): boolean;

Parameters

List of parameters
Parameter Type Description
key TKey

Returns:

boolean

remove

Remove the Promise for the given key, returning true if it was found and removed

Signature:

remove(key: TKey): boolean;

Parameters

List of parameters
Parameter Type Description
key TKey

Returns:

boolean