ts-promise-cache
Loading cache for promises. Does not suffer from thundering herds problem (aka cache stampede).
Usage
The constructor takes a loader that loads missing entries. By default rejected Promises are not kept in the cache.
loader: (key: string) => Promise
; ; expectvalue.to.eq"value";
Config
The second constructor argument is an optional config (Partial<CacheConfig> config is ok).
Using Partial<CacheConfig> it looks like:
new PromiseCacheloader, ;
Single promise cache
In case you have a single value to cache, use singlePromiseCache factory.
// singlePromiseCache<T>(loader: () => Promise<T>, config?: Partial<CacheConfig<T>>): () => Promise<T> ; ;expectvalue.to.eq"value";
TTL Expiry
The default behavior is for values to remain in the cache for 'ttl' milliseconds since the last access. It is sometimes useful for cache expiry to be relative to when the cached value was first created (or last changed).
For this situation, set ttlAfter:"WRITE":
new PromiseCacheloader, ;
Statistics
stats() returns statistics:
Set value
You can set a value directly
cache.set"key", "value";expectawait cache.get"key".to.eq"value";
Retry
Retry can by implemented by using ts-retry-promise
;;expectawait cache.get"key".to.eq"value";