Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103993
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Caching function results |
| Date | 2016-03-03 13:59 -0700 |
| Message-ID | <mailman.165.1457038833.20602.python-list@python.org> (permalink) |
| References | <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org> |
On Thu, Mar 3, 2016 at 1:28 PM, Pavel Volkov <sailor@lists.xtsubasa.org> wrote:
> 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.
>
> I intend to do it this way:
>
> # module 'cache.py'
>
> class Cache:
> def save_result(self, handle, args):
> """Save calculated result to cache."""
> <...>
> def load_result(self, handle, args, result):
> """Load previously calculated result from cache.
> Return None is it's unavailable."""
> <...>
> def save_to_file(self, filename):
> """Save all cached data to a file."""
> def __init__(self, filename=None):
> # Optionally loads previously cached data from a file
> def clear(self):
> <...>
>
>
> # module 'calculations.py'
>
> import cache
> _cache = cache.Cache()
>
> def big_task(arg1, arg2, arg3=None):
> cached_value = _cache.load_result('big_task', (arg1, arg2, arg3))
> if cached_value is not None:
> return cached_value
> <do the normal calculations, no return operators here>
> result = <...>
> _cache.save_result('big_task', (arg1, arg2, arg3), result)
> return result
>
> The arguments and return values are almost always:
> * ints
> * floats
> * tuple or lists of ints or floats
>
> I think Cache object will store data in a dictionary.
> I'll convert lists to tuples before storing them.
>
> 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.
>
> Do you like this design or maybe there's a better way with Python's included
> batteries?
The idiomatic way is to use functools.lru_cache like so:
import functools
@functools.lru_cache(maxsize=128)
def big_task(arg1, arg2, arg3=None):
...
This does not give you the option to save the cache to a file however,
and the maxsize is specified in entries, not MB.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Caching function results Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-03 13:59 -0700
csiph-web