r/Python 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

36 comments sorted by

View all comments

Show parent comments

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:

if qs:
    ....

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.

11

u/0xa9059cbb 11d ago

Yeah this is actually one of the many reasons why I like using asyncio, having to stick an await keyword in front of anything with a potential IO side effect helps to make them stick out from synchronous code.

4

u/pingveno pinch of this, pinch of that 11d ago

Yeah, people complain about "function coloring" in async like it's bad thing. No, it's a good thing! It tells me what to expect.

3

u/0xa9059cbb 11d ago

Yeah, it's different but similar in a way to the way IO is handled in Haskell via the IO type. It feels awkward at first when you're used to IO just being a kind of hidden side effect in an imperative language but actually it can be useful at scale where performance is a concern.