Download src/common/utils/cache.js from moustafa7/gymco: direct link, hf CLI and curl.
- Browser
- Download file 612 Bytes
-
https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/common/utils/cache.js
- Command line
-
hf download hf://spaces/moustafa7/gymco/src/common/utils/cache.js
-
curl -L -o cache.js https://huggingface.co/spaces/moustafa7/gymco/resolve/main/src/common/utils/cache.js
612 Bytes
| import { redis } from '../../config/redis.js'; | |
| export const cached = async (key, ttlSeconds, fetchFn) => { | |
| if (!redis) return fetchFn(); | |
| try { | |
| const hit = await redis.get(key); | |
| if (hit) return JSON.parse(hit); | |
| } catch { | |
| return fetchFn(); | |
| } | |
| const value = await fetchFn(); | |
| try { | |
| await redis.setEx(key, ttlSeconds, JSON.stringify(value)); | |
| } catch { | |
| // cache write failure is non-critical | |
| } | |
| return value; | |
| }; | |
| export const invalidateCache = async (...keys) => { | |
| if (!redis || !keys.length) return; | |
| try { | |
| await redis.del(keys); | |
| } catch { | |
| // non-critical | |
| } | |
| }; | |