Download cache.py from mgbam/AI_SQL: direct link, hf CLI and curl.
- Browser
- Download file 310 Bytes
-
https://huggingface.co/spaces/mgbam/AI_SQL/resolve/5a3712a06459932e8cfb0a4ba14be1edeb498d6c/cache.py
- Command line
-
hf download hf://spaces/mgbam/AI_SQL@5a3712a06459932e8cfb0a4ba14be1edeb498d6c/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/mgbam/AI_SQL/resolve/5a3712a06459932e8cfb0a4ba14be1edeb498d6c/cache.py
310 Bytes
| import redis, hashlib, json, os | |
| r = redis.Redis.from_url(os.getenv("REDIS_URL", "redis://localhost:6379/0")) | |
| def cached(key: str, fn): | |
| h = hashlib.sha256(key.encode()).hexdigest() | |
| if (val := r.get(h)): | |
| return json.loads(val) | |
| out = fn() | |
| r.setex(h, 3600, json.dumps(out)) | |
| return out | |