Skip to main content

A simple decorator to cache the results of function calls.

Installation

pip install cache_em_all

Example

from cache_em_all import Cachable

@Cachable("answer.json")
def answer_to(question):
    if question == "life":
        import time
        time.sleep(236500000000000)
        return 42

answer_to("life") # Takes 7.5 million years
answer_to("life") # Pretty much instant

After the first call to answer_to, the result of the function is stored in a file cache/answer/answer__life.json. When the function is called again (with the same parameters), instead of executing the function, the decorator will get the result from the file.

Advanced usage

File types

Various file types are supported.

extension

Description

csv

Uses pandas to save the result in a csv file. The return value for the function must be a DataFrame or Series.

json

Saves the result in a json file. Useful for lists, dictionaries and primitive types.

pkl

Pickles the return value. Return type can be just about anything. May not work well for large (>~2GB) files

pa

Uses pyarrow to save files. This is generally faster than pkl and supports larger files (tested up to 50GB)

Versioning

The Cachable decorator can accept a version number. This is useful for when you update a function. For example,

You had the following code:

from cache_em_all import Cachable

@Cachable("add.json")
def add(x, y):
    return x + x

This is a bug (should be x+y, not x+x), but you’ve run this function multiple times and there are lots of cached results. Rather than manually deleting the cache folder, you can bump the version number (version numbers start at 0).

from cache_em_all import Cachable

@Cachable("add.json", version=1)
def add(x, y):
    return x + y

Now next time the function is called it will invalidate the cache and re-run the function.

Do not use this feature in multi-processing code because the writes to the version file do not (yet) use locks.

Folder

By default all cached files are stored in a folder called cache. This can be changed for each function by passing folder to Cachable.

from cache_em_all import Cachable

@Cachable("add.json", folder="/mnt/ram/fastcache")
def add(x, y):
    return x + y

You can also change the default cache dir so that all cache files are by default saved elsewhere.

from cache_em_all import Cachable, set_cache_dir
set_cache_dir("/mnt/huge_hdd")

@Cachable("add.json")
def add(x, y):
    return x + y

Disable cache

You can also disable the cache by setting use to False.

from cache_em_all import Cachable

@Cachable("add.json", use=False)
def add(x, y):
    return x + y

This can be useful for debugging or optmizing the function.

Release files for cache-em-all 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cache-em-all 0.2.2
File Size Uploaded
cache-em-all-0.2.2.tar.gz 16.6 kB Details

Release files / cache-em-all-0.2.2.tar.gz

Download URL cache-em-all-0.2.2.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
db24478b027a3c096f3fe0cd7dd2c85817d8fb697b04610bdde0e07d57460868
BLAKE2b-256 checksum
How to use checksums
d3926e01eba792c5d1895430ee214c06ef24934f16254d72013499412b1c0c25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page