Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103992

Re: Caching function results

From "Martin A. Brown" <martin@linux-ip.net>
Newsgroups comp.lang.python
Subject Re: Caching function results
Date 2016-03-03 12:59 -0800
Message-ID <mailman.164.1457038775.20602.python-list@python.org> (permalink)
References <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org>

Show all headers | View raw


Greetings Pavel,

> Suppose, I have some resource-intensive tasks implemented as 
> functions in Python. Those are called repeatedly in my program. 
> It's guranteed that a call with the same arguments always produces 
> the same return value. I want to cache the arguments and return 
> values and in case of repititive call immediately return the 
> result without doing expensive calculations.

Great problem description.  Thank you for being so clear.

[I snipped sample code...]

This is generically called memoization.

> Do you like this design or maybe there's a better way with 
> Python's included batteries?

In Python, there's an implementation available for you in the 
functools module.  It's called lru_cache.  LRU means 'Least Recently 
Used'.

> I'd also like to limit the size of the cache (in MB) and get rid 
> of old cached data. Don't know how yet.

You can also limit the size of the lru_cache provided by the 
functools module.  For this function, the size is calculated by 
number of entries--so you will need to figure out memory size to 
cache entry count.

Maybe others who have used functools.lru_cache can help you with how 
they solved the problem of mapping entry count to memory usage.

Good luck,

-Martin

 [0] https://docs.python.org/3/library/functools.html#functools.lru_cache

-- 
Martin A. Brown
http://linux-ip.net/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Caching function results "Martin A. Brown" <martin@linux-ip.net> - 2016-03-03 12:59 -0800

csiph-web