Download src/lib/utils/cache.ts from huggingface/inference-playground: direct link, hf CLI and curl.
- Browser
- Download file 631 Bytes
-
https://huggingface.co/spaces/huggingface/inference-playground/resolve/refs%2Fpr%2F13/src/lib/utils/cache.ts
- Command line
-
hf download hf://spaces/huggingface/inference-playground@refs/pr/13/src/lib/utils/cache.ts
-
curl -L -o cache.ts https://huggingface.co/spaces/huggingface/inference-playground/resolve/refs%2Fpr%2F13/src/lib/utils/cache.ts
631 Bytes
| 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; | |
| }; | |
| } | |