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 | |