Spaces:
Paused
Paused
Download src/utils/cache.ts from mojaalagevai/consumet: direct link, hf CLI and curl.
- Browser
- Download file 981 Bytes
-
https://huggingface.co/spaces/mojaalagevai/consumet/resolve/main/src/utils/cache.ts
- Command line
-
hf download hf://spaces/mojaalagevai/consumet/src/utils/cache.ts
-
curl -L -o cache.ts https://huggingface.co/spaces/mojaalagevai/consumet/resolve/main/src/utils/cache.ts
981 Bytes
| import { Redis } from 'ioredis'; | |
| /* eslint-disable import/no-anonymous-default-export */ | |
| /* | |
| TLDR; " Expires " is seconds based. for example 60*60 would = 3600 (an hour) | |
| */ | |
| const fetch = async <T>(redis: Redis, key: string, fetcher: () => T, expires: number) => { | |
| const existing = await get<T>(redis, key); | |
| if (existing !== null) return existing; | |
| return set(redis, key, fetcher, expires); | |
| }; | |
| const get = async <T>(redis: Redis, key: string): Promise<T> => { | |
| console.log('GET: ' + key); | |
| const value = await redis.get(key); | |
| if (value === null) return null as any; | |
| return JSON.parse(value); | |
| }; | |
| const set = async <T>(redis: Redis, key: string, fetcher: () => T, expires: number) => { | |
| console.log(`SET: ${key}, EXP: ${expires}`); | |
| const value = await fetcher(); | |
| await redis.set(key, JSON.stringify(value), 'EX', expires); | |
| return value; | |
| }; | |
| const del = async (redis: Redis, key: string) => { | |
| await redis.del(key); | |
| }; | |
| export default { fetch, set, get, del }; | |