tkem / cachetools

Extensible memoizing collections and decorators
MIT License
2.34k stars 163 forks source link

Add on expire method #320

Closed Amedeo91 closed 2 months ago

Amedeo91 commented 4 months ago

Hello there, the goal of this PR is to provide a callback to process expired cached objects, such as connection to databases or similar, which needs to be closed to avoid memory leaks. That would be the only way without the need to override the expire function. Notice that: it could make sense to extend that to delitem or pop method, however, those can be easily overridden without the need to copy-paste the whole implementation. Thanks

tkem commented 2 months ago

Thanks for your interest, but instead of a callback-based solution introducing another constructor argument, I opted for expire() returning the expired items, which can be examined by a subclass overriding this method. This is more in line with the existing popitem() documentation. However, thanks again for submitting this, since it finally made me actually sit down and do something about this issue ;-)

Amedeo91 commented 2 months ago

Hello there, that knyou for having a look. Did you merge already your implementation? I am curious to see it! Thanks

tkem commented 2 months ago

Not yet, please see https://github.com/tkem/cachetools/tree/dev/v5.5.0 and feel free to comment.