Caching function results

Pavel Volkov sailor at lists.xtsubasa.org
Thu Mar 3 15:28:32 EST 2016


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?




More information about the Python-list mailing list