mirror of https://github.com/wg-easy/wg-easy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
494 B
29 lines
494 B
type Opts = {
|
|
/**
|
|
* Expiry time in milliseconds
|
|
*/
|
|
expiry: number;
|
|
};
|
|
|
|
/**
|
|
* Cache function for 1 hour
|
|
*/
|
|
export function cacheFunction<T>(fn: () => T, { expiry }: Opts): () => T {
|
|
let cache: { value: T; expiry: number } | null = null;
|
|
|
|
return (): T => {
|
|
const now = Date.now();
|
|
|
|
if (cache && cache.expiry > now) {
|
|
return cache.value;
|
|
}
|
|
|
|
const result = fn();
|
|
cache = {
|
|
value: result,
|
|
expiry: now + expiry,
|
|
};
|
|
|
|
return result;
|
|
};
|
|
}
|
|
|