Skip to main content

cache_to_disk

Local disk caching decorator for python functions with auto-invalidation.

This is intended to cache functions that both take a long time to run, and have return values that take up too much memory to cache in-memory with redis. The results of the function are pickled and saved to a file, and then unpickled and returned the next time the function is called. The caching is argument specific, so if the function is called with different arguments, the function will be run again. The caching decorator accepts an integer representing the number of days to cache the function for. After this many days, the file for that function will be deleted the next time this module is imported.

Installation

pip install cache_to_disk

Functions:

cache_to_disk(n_days_to_cache) delete_disk_caches_for_function(function_name) delete_old_disk_caches()

Examples:

cache_to_disk

"""
This example caches the function "my_function" for 3 days.
"""

from cache_to_disk import cache_to_disk

@cache_to_disk(3)
def my_function():
    to_return = []
    for i in range(10000):
        for j in range(i):
            to_return.append(i * j ** .23)
    return to_return

delete_disk_caches_for_function

"""
This example invalidates all of the caches for the function "my_function". The function will be invalidated automatically, but this should be used when the function definition has been changed and you want it to re-run.
"""

from cache_to_disk import delete_disk_caches_for_function
delete_disk_caches_for_function('my_function')

runtime_accounting

"""
This example shows how to check the run-time cache accounting, which shows hits, misses and nocache events
"""

from cache_to_disk import cache_to_disk

@cache_to_disk(3)
def query_registrar(host, port, query):
    socket = tcp_connect(host, port)
    socket.send(query)
    response = b''
    while True:
        buf = read_wrapper(socket)
        if buf is None:
            break
        response += buf
    return response    

query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
print(query_registrar.cache_info())

nocache

"""
This example shows how to inhibit caching depending on certain conditions, such as a network failure while also returning a value
"""
from cache_to_disk import cache_to_disk, NoCacheCondition
from random import randint
@cache_to_disk(3)
def query_registrar(host, port, query):
    socket = tcp_connect(host, port)
    socket.send(query)
    response = b''
    while True:
        try:
            if randint(0, 5) > 3:
                # Simulate a spurious failure like SIGPIPE/EPIPE
                raise socket.error
            buf = read_wrapper(socket)

            if buf is None:
                break
            response += buf
        except socket.error:
            # To the user, functionalliy requivalent to `return response` except
            # the result is not cached, so it can be retried immediately or later
            raise NoCacheCondition(function_value=response)
    return response

query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
query_registrar('whois.verisign-grs.com', 43, b'test.com')
print(query_registrar.cache_info())

Metadata

Release files for cache-to-disk 2.0.0

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-to-disk 2.0.0
File Size Uploaded
cache_to_disk-2.0.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cache-to-disk 2.0.0
File Interpreter ABI Platform
cache_to_disk-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / cache_to_disk-2.0.0.tar.gz

Download URL cache_to_disk-2.0.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
79e19ea9b72eedc5cec83bb8aa55374afc671493e7d13d541f3b63eb3a13fb32
BLAKE2b-256 checksum
How to use checksums
22df9712d98cadf4dfab259b09ea2972f5585f4a6909d19fe60be2b7adb8823d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.12.5 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

Release files / cache_to_disk-2.0.0-py3-none-any.whl

Download URL cache_to_disk-2.0.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea5afe13d4284cb4a06169b0807fbc60547cbe19c54563bf90e1d44f24029481
BLAKE2b-256 checksum
How to use checksums
874fbd5da67f8f509c8e34cab16f6cc88a843d9b15b3631381a09d98f4f1d70f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.12.5 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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