AI_SQL / cache.py
mgbam's picture
Create cache.py
123bf53 verified
raw
history blame contribute delete
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