File size: 310 Bytes
123bf53
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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