File size: 631 Bytes
73e0644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type WithCacheOptions = {
	// Lifespan of the cache in milliseconds
	// @default 10 minutes
	lifespan?: number;
};

export function withCache<Args extends unknown[], Return>(
	fn: (...args: Args) => Return,
	options?: WithCacheOptions,
): (...args: Args) => Return {
	const { lifespan = 1000 * 60 * 10 } = options ?? {};

	const cache = new Map<string, Return>();

	return (...args: Args) => {
		const key = JSON.stringify(args);
		if (cache.has(key)) {
			return cache.get(key) as Return;
		}

		const value = fn(...args);
		cache.set(key, value);
		setTimeout(() => {
			cache.delete(key);
		}, lifespan);

		return value;
	};
}