Skip to main content

Cache functions with pickle and files

Project description

Cache the results of a function.

Simple use

Decorate the function with @cache(cachedir, **kwargs). For example,

def is_prime(number):
    for n in range(2, number):
        if number % n == 0:
            return False
    return True

Fancier uses

Non-default directory

If you pass no arguments to cache, as in the example above, the cache will be stored in a directory named after the function. To set a different directory, pass it as an argument.

def is_prime(number):
    for n in range(2, number):
        if number % n == 0:
            return False
    return True

I recommend storing your caches in dotted directories under your home directory, as you see above.


The kwargs get passed to the pickle_warehouse.Warehouse, so you can do fun things like changing the serialization function.

@cache('~/.http', serializer = pickle_warehouse.serializers.identity)
def get(url):
    return requests.get(url).text

Read more about the keyword arguments on the pickle-warehouse page.

Non-identifying arguments

If you want to pass an argument but not use it as an identifier, pass a non-keyword argument; those get passed along to the function but don’t form the identifier. For example,

def get(url, auth = None):
    return requests.get(url, auth = auth)

get('', auth = ('username', 'password')

Refreshing the cache

I find that I sometimes want to refresh the cache for a particular file, only. This is usually because an error occurred and I have fixed the error or because I am downloading files from a website that doesn’t work very well.

It would be nice to have a convenient way of refreshing these files. Until then, I suggest that you log the arguments to the function call that yields the cached error and then delete the corresponding file.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for picklecache, version 0.0.5
Filename, size File type Python version Upload date Hashes
Filename, size picklecache-0.0.5.tar.gz (2.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page