mgbam commited on
Commit
123bf53
·
verified ·
1 Parent(s): f2f4930

Create cache.py

Browse files
Files changed (1) hide show
  1. cache.py +10 -0
cache.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import redis, hashlib, json, os
2
+ r = redis.Redis.from_url(os.getenv("REDIS_URL", "redis://localhost:6379/0"))
3
+
4
+ def cached(key: str, fn):
5
+ h = hashlib.sha256(key.encode()).hexdigest()
6
+ if (val := r.get(h)):
7
+ return json.loads(val)
8
+ out = fn()
9
+ r.setex(h, 3600, json.dumps(out))
10
+ return out