Spaces:
Running
Running
Download source/teaql-ts/src/core/local-cache.ts from teaql/browser-sqlite-playground: direct link, hf CLI and curl.
- Browser
- Download file 952 Bytes
-
https://huggingface.co/spaces/teaql/browser-sqlite-playground/resolve/main/source/teaql-ts/src/core/local-cache.ts
- Command line
-
hf download hf://spaces/teaql/browser-sqlite-playground/source/teaql-ts/src/core/local-cache.ts
-
curl -L -o local-cache.ts https://huggingface.co/spaces/teaql/browser-sqlite-playground/resolve/main/source/teaql-ts/src/core/local-cache.ts
952 Bytes
| type LocalCacheEntry = { | |
| value: unknown; | |
| expiresAt?: number; | |
| }; | |
| /** Process-local cache with optional TTL in seconds. */ | |
| export class LocalCache { | |
| private readonly entries = new Map<string, LocalCacheEntry>(); | |
| put(key: string, value: unknown, timeToLiveInSeconds?: number): void { | |
| const expiresAt = timeToLiveInSeconds !== undefined && timeToLiveInSeconds > 0 | |
| ? Date.now() + timeToLiveInSeconds * 1000 | |
| : undefined; | |
| this.entries.set(key, { value, expiresAt }); | |
| } | |
| get<T>(key: string): T | undefined { | |
| const entry = this.entries.get(key); | |
| if (entry === undefined) return undefined; | |
| if (entry.expiresAt !== undefined && Date.now() >= entry.expiresAt) { | |
| this.entries.delete(key); | |
| return undefined; | |
| } | |
| return entry.value as T; | |
| } | |
| remove(key: string): void { | |
| this.entries.delete(key); | |
| } | |
| clear(): void { | |
| this.entries.clear(); | |
| } | |
| } | |
| export const localCache = new LocalCache(); | |