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


Groups > comp.lang.python > #103989

Caching function results

From Pavel Volkov <sailor@lists.xtsubasa.org>
Newsgroups comp.lang.python
Subject Caching function results
Date 2016-03-03 23:28 +0300
Message-ID <mailman.161.1457037388.20602.python-list@python.org> (permalink)

Show all headers | View raw


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?

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


Thread

Caching function results Pavel Volkov <sailor@lists.xtsubasa.org> - 2016-03-03 23:28 +0300

csiph-web