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
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
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
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
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
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
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
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
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
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
Parameter | Type | Description |
---|---|---|
key | TKey |
Returns:
boolean