SebastiaanZ / async-rediscache

An easy-to-use asynchronous redis-backed caching utility
18 stars 3 forks source link

Support Expiry For Entries #17

Open HassanAbouelela opened 2 years ago

HassanAbouelela commented 2 years ago

Adding a built-in way to add expiry to the entries added by the library would be useful for various purposes.

Looking at the following comment from @SebastiaanZ:

As discussed above, it would only work on an entire RedisCache at once because of Redis limitations.

it doesn't seem simple to do natively, but I think it can be done in python.

My suggested implementation is to add an expiry datetime to entries upon insertion and schedule the deletion in python. This would probably require a start-up task to reschedule the deletions too. The major downsides I see to this:

I've more or less implemented this (minus the scheduled deletion) in python-discord/bot#1961 using the program's scheduler, but it would be nice to have natively.

MarkKoz commented 2 years ago

Alternatively, it can be done using SET instead of HSET and making the key a composite of the namespace and the actual key. The class would take care of inserting the namespace automatically for any operations that have the key as an argument. A delimiting character would have to be reserved to prevent ambiguities.

https://github.com/python-discord/bot/blob/main/bot/exts/info/doc/_redis_cache.py#L14-L59 did something similar, though it used HSET still because it actually wanted expiry for a set of keys rather than for just 1 key.

HassanAbouelela commented 2 years ago

I spoke to Sebastiaan about this issue, and it seems the overhead and complexity of managing the scheduling in python is not desirable here, but I think that leaves Mark's set solution.