chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
49 lines • 1.66 kB
JavaScript
const NOT_CACHED = Symbol('not-cached');
export class TtlCache {
fetcher;
ttlMs;
cachedValue = NOT_CACHED;
expiresAt = 0;
pendingPromise = null;
constructor(fetcher, ttlSeconds) {
this.fetcher = fetcher;
this.ttlMs = ttlSeconds * 1000;
}
/**
* Fetches a value, optionally using the cache.
*
* @param useCache – if true, return the cached value when still fresh;
* if false, always re-fetch, cache & return the new value.
*/
async get(useCache) {
const now = Date.now();
// 1) If we’re allowed to use cache, and it’s still valid, just return it.
if (useCache && this.cachedValue !== NOT_CACHED && now < this.expiresAt) {
return this.cachedValue;
}
// 2) If there’s already a fetch in flight, join it (no double-fetch).
if (this.pendingPromise) {
return this.pendingPromise;
}
// 3) Kick off a (new or forced) fetch, cache the result & reset TTL.
this.pendingPromise = (async () => {
try {
const result = await this.fetcher();
this.cachedValue = result;
this.expiresAt = Date.now() + this.ttlMs;
return result;
}
finally {
this.pendingPromise = null;
}
})();
return this.pendingPromise;
}
/** Clear both the cached value and any in-flight fetch. */
clear() {
this.cachedValue = NOT_CACHED;
this.expiresAt = 0;
this.pendingPromise = null;
}
}
//# sourceMappingURL=TtlCache.js.map