Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Add cache last fetch success status
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed May 21, 2021
1 parent 684603d commit c1bf3bf
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/helpers/fetchCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ICache<T> {
fetchedFor: number;
cacheFor: number;
accessedSinceFetch: number,
lastFetchSuccess: boolean,
data: T | null;
fetching: AtomicBool;
timer: ReturnType<typeof setInterval> | null;
Expand All @@ -30,6 +31,7 @@ const newCacheEntry = (cacheFor = 0n): ICache<unknown> => ({
fetchedFor: 0,
accessedSinceFetch: 0,
cacheFor: toMs(cacheFor),
lastFetchSuccess: true,
data: null,
fetching: new AtomicBool(),
timer: null,
Expand Down Expand Up @@ -91,6 +93,7 @@ export const cachedFetcher = <T>(
function cacheSet(cacheKey: CacheKey, key: "data", value: Cache["data"]): void;
function cacheSet(cacheKey: CacheKey, key: "fetchedFor", value: Cache["fetchedFor"]): void;
function cacheSet(cacheKey: CacheKey, key: "accessedSinceFetch", value: Cache["accessedSinceFetch"]): void;
function cacheSet(cacheKey: CacheKey, key: "lastFetchSuccess", value: Cache["lastFetchSuccess"]): void;
function cacheSet(cacheKey: CacheKey, key: "timer", value: Cache["timer"]): void;
function cacheSet(cacheKey: CacheKey, key: keyof Cache, value): void {
if (!(cacheKey in cache)) {
Expand All @@ -104,6 +107,7 @@ export const cachedFetcher = <T>(
function cacheGet(cacheKey: CacheKey, key: "data"): Cache["data"];
function cacheGet(cacheKey: CacheKey, key: "fetching"): Cache["fetching"];
function cacheGet(cacheKey: CacheKey, key: "accessedSinceFetch"): Cache["accessedSinceFetch"];
function cacheGet(cacheKey: CacheKey, key: "lastFetchSuccess"): Cache["lastFetchSuccess"];
function cacheGet(cacheKey: CacheKey, key: "timer"): Cache["timer"];
function cacheGet(cacheKey: CacheKey, key: keyof Cache) {
if (!(cacheKey in cache)) {
Expand Down Expand Up @@ -220,8 +224,13 @@ export const cachedFetcher = <T>(

setData(key, data, toMs(endTime - startTime));
resetAccessed(key);
cacheSet(key, "lastFetchSuccess", true);

return data;
} catch {
cacheSet(key, "lastFetchSuccess", false);

return getData(key);
} finally {
setFetching(key, false);
setTimer(key, fetchData);
Expand Down

0 comments on commit c1bf3bf

Please sign in to comment.