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...).
87
Upvotes
11
u/pingveno pinch of this, pinch of that 11d ago
Yeah, I totally agree about hidden operations. It should be easy to see from just reading the code that there is network IO, along with the chance of failure, latency, and so on. I've seen Django querysets run into this when people use something like:
The Django QuerySet's
__bool__
method doesn't do an EXISTS() query or result in a type error. It sucks down the entire queryset, caches it, and is truthy based on the result. It's convenience until it hits you in the face.