Skip to content

Instantly share code, notes, and snippets.

@tobbbe
Last active December 15, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobbbe/fb1ebf2a760c09e5ecefe04ab61b02cb to your computer and use it in GitHub Desktop.
Save tobbbe/fb1ebf2a760c09e5ecefe04ab61b02cb to your computer and use it in GitHub Desktop.
const debug = true;
function debugLog(message) {
if (debug) {
console.log(message);
}
}
let sharedCached: { [key: string]: IKachelerCacheItem<unknown> } = {};
export const kacheler = {
create: <T,>(validTimeInMilliseconds: number): IKachelerCacheItem<T> => {
let value = null;
function get(): T {
return value;
}
function set(newValue: T) {
value = newValue;
cache.updateEndTime();
}
function shouldBust() {
return kacheler.shouldBust(cache.expiresAt);
}
const cache: IKachelerCacheItem<T> = {
get,
set,
validTimeInMilliseconds,
shouldBust,
updateEndTime: () => {
cache.expiresAt = kacheler.calcEndTimeMilliseconds(
cache.validTimeInMilliseconds
);
debugLog('set cache, expires at: ' + cache?.expiresAt);
},
};
return cache;
},
calcEndTimeMilliseconds: (milliseconds: number): Date => {
return new Date(Date.now() + milliseconds);
},
calcEndTimeSeconds: (seconds: number): Date => {
return kacheler.calcEndTimeMilliseconds(
kacheler.toMilliseconds.fromSeconds(seconds)
);
},
calcEndTimeMinutes: (minutes: number): Date => {
return kacheler.calcEndTimeMilliseconds(
kacheler.toMilliseconds.fromMinutes(minutes)
);
},
calcEndTimeHours: (hours: number): Date => {
return kacheler.calcEndTimeMilliseconds(
kacheler.toMilliseconds.fromHours(hours)
);
},
calcEndTimeDays: (days: number): Date => {
return kacheler.calcEndTimeMilliseconds(
kacheler.toMilliseconds.fromDays(days)
);
},
toMilliseconds: {
fromSeconds: (minutes: number): number => minutes * 1000,
fromMinutes: (minutes: number): number => minutes * 60000,
fromHours: (minutes: number): number => minutes * 3600000,
fromDays: (minutes: number): number => minutes * 86400000,
},
shouldBust: (endTime: Date): boolean => {
return !endTime || endTime < new Date();
},
getOrCreate: <T,>(
key: string,
validTimeInMilliseconds: number
): IKachelerCacheItem<T> => {
if (!sharedCached[key]) {
sharedCached[key] = kacheler.create<T>(validTimeInMilliseconds);
}
return sharedCached[key] as IKachelerCacheItem<T>;
},
get: <T,>(key: string): IKachelerCacheItem<T> => {
return sharedCached[key] as IKachelerCacheItem<T>;
},
clearAll: (): void => {
sharedCached = {};
},
};
export interface IKachelerCacheItem<T> {
get(): T;
set(value: T): void;
expiresAt?: Date;
validTimeInMilliseconds: number;
shouldBust(): boolean;
updateEndTime(): void;
}
// EXAMPLE
// export async function GetFacilities(): Promise<FacilityDto[]> {
// const cache = kacheler.getOrCreate<FacilityDto[]>(
// '/facilities',
// kacheler.toMilliseconds.fromSeconds(30)
// );
//
// if (cache.shouldBust()) {
// const facilitiesRes = await fetchApi('/facilities');
// cache.set(facilitiesRes.ok ? await facilitiesRes.json() : []);
// }
// return cache.get();
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment