classification
Title: Add callback to functools.lru_cache
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: aj, ncoghlan, rhettinger
Priority: normal Keywords:

Created on 2016-09-13 04:53 by aj, last changed 2016-09-13 05:50 by aj. This issue is now closed.

Messages (3)
msg276171 - (view) Author: Alex Jurkiewicz (aj) Date: 2016-09-13 04:53
I'd like to add support for functools.lru_cache to have a callback parameter that runs every time an item is evicted from the cache. I've written a small prototype here:

https://github.com/alexjurkiewicz/cpython/commit/2f788387466720d47f2c82242ac33b107b0bf470

The prototype is incomplete:
* No C implementation
* Doesn't run on lru_cache.cache_clear()
* It would be nice to pass through args/kwargs for each evicted item

Comments welcome!
msg276179 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-13 05:41
Alex, I would like to pass on this feature request and keep the lru_cache API focused on its core task.  As it stands now, it was already difficult to fight reentrancy problems.

FWIW, it is very easy to roll your own LRU variants using collections.OrderedDict.  Also, if you're inclined, you can post Python recipes and at the ASPN Python website -- that is where I post my LRU variants.
msg276180 - (view) Author: Alex Jurkiewicz (aj) Date: 2016-09-13 05:50
OK, thanks for the feedback. For future searchers I am using the pylru module instead:
https://pypi.python.org/pypi/pylru
History
Date User Action Args
2016-09-13 05:50:53ajsetmessages: + msg276180
2016-09-13 05:41:16rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg276179
2016-09-13 04:59:09berker.peksagsetnosy: + rhettinger, ncoghlan
stage: needs patch

versions: - Python 3.3, Python 3.4, Python 3.5, Python 3.6
2016-09-13 04:53:35ajcreate