[Python-ideas] functools.lru_cache manual cache modification

Cameron Simpson cs at zip.com.au
Mon Dec 1 23:41:40 CET 2014

On 01Dec2014 17:34, Constantin Berhard <constantin at exxxtremesys.lu> wrote:
>One strength of the functools.lru_cache lies in caching results of calls
>initiated by the function itself (i.e. recursive call results).
>However because of the exception, the intermediate results from the tail
>recursion don't end up in the cache, if my tail call optimization is
>used together with lru_cache.
>So I would like to be able to manually add argument-result pairs in the
>cache. A manual lookup is not needed for my purpose, but I propose that
>there should be methods to
>1. add argument-result pairs to the cache
>2. lookup if there is a result for given arguments
>3. lookup the result for given arguments if it exists (exception thrown
>4. invalidate specific cache entries (at the moment you can only
>invalidate the cache as a whole through func.cache_clear())
>What do you think?

I'm kind of +1 on this, but I'd be for a different exposure. Let lru_cache 
expose a mapping of the cache. Once you have that mapping, expecially if it is 
just a dict, everything else comes for free.

In fact, I'd advocate moving the LRU cache supporting object off into 
collections and building functools.lru_cache on top of that. Use case: I had to 
write my own LRU cache bcause I wasn't just wrapping a function. Having that 
available as a standalone object from collections which looked like a lossy 
mapping (i.e.  things vanish from the mapping when the cache overflows) would 
be immensely useful.

Cameron Simpson <cs at zip.com.au>

The first ninety percent of the task takes ninety percent of the time, and
the last ten percent takes the other ninety percent.

More information about the Python-ideas mailing list