async-rediscache
async-rediscache copied to clipboard
Support Expiry For Entries
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:
- It's not going to work quite right, since the deletions only happen when the library is running. That is not great for end-users, but it might not also be the end of the world if it's clear up front.
- It requires the insertion of an extra datetime to power the rescheduling.
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.
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.
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.