@croct/cache
Version:
An abstraction layer for caching.
26 lines (25 loc) • 1.2 kB
TypeScript
import { CacheLoader, CacheProvider } from './cacheProvider';
/**
* A cache that shares in-flight requests between callers.
*
* The scenario target by this cache is as follows:
* - Caller 1 requests the entry key A.
* - While the underlying cache is processing that request, possibly waiting
* for IO, a caller 2 requests the entry for the same key A.
* - This cache will return a reference to the same Promise returned to caller 1.
*
* Wrapping a cache with a `SharedInFlightCache` ensures that the same entry
* is not created twice. In the example above, if the entry was not present in
* the cache the loader would be called twice and, if the cache auto-saves,
* the cache entry would also be written twice.
* With this wrapper, the loaders would only be called once, and the entry
* would also be saved once, if the cache auto-saves.
*/
export declare class SharedInFlightCache<T> implements CacheProvider<string, T> {
private readonly inner;
private readonly pending;
constructor(inner: CacheProvider<string, T>);
get(key: string, loader: CacheLoader<string, T>): Promise<T>;
set(key: string, value: T): Promise<void>;
delete(key: string): Promise<void>;
}