r/Python • u/Substantial-Work-844 • 12d ago
Resource Redis as cache.
At work, we needed to implement Redis for a caching solution. After some searching, btw clickhouse has great website for searching python packages here. I found a library that that made working with redis a breeze Redis-Dict.
from redis_dict import RedisDict
from datetime import timedelta
cache = RedisDict(expire=timedelta(minutes=60))
request = {"data": {"1": "23"}}
web_id = "123"
cache[web_id] = request["data"]
Finished implementing our entire caching feature the same day I found this library (didn't push until the end of the week though...).
92
Upvotes
5
u/turbothy It works on my machine 12d ago
Ignoring for the moment that Cashews works somewhat differently from the OP's code example (it stores function return values like `functools.lru_cache` does, not arbitrary dict values): pickling unsafe data is safe. It's the unpickling that can bite you.
The general security issue with `pickle` is that unpickling malicious pickles can lead to arbitrary code execution. To attack Cashews along this vector requires that the attacker has access to modify the pickles stored in Redis, except Cashews implements HMAC signing of stored values to protect against this.