Caching function results

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 3 15:59:44 EST 2016


On Thu, Mar 3, 2016 at 1:28 PM, Pavel Volkov <sailor at 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.



More information about the Python-list mailing list