Skip to main content

disk_cache_data decorator

Fully configurable decorator with functionnality as Streamlit st.cache_data with local disk storage as backend.

Installation

pip install mysiar-disk-cache-data

Usage

disk_cache_data

ttl accepts:

  • None or no ttl at all - the entry never expires, same as st.cache_data
  • a number of seconds, e.g. 30
  • a string, e.g. "30s", "5m", "2h", "1d", "1d 4h"
  • 0 - caching is bypassed and the function runs every call

Configuration by environment variables:

  • DISK_CACHE_DISABLED=1 - disable caching
  • DISK_CACHE_DEBUG=1 - enable debug logging (currently by print statements)
  • DISK_CACHE_DIR=/path/to/cache/dir - set custom cache directory (default is /tmp/disk_cache)
  • DISK_CACHE_NAMESPACE - subfolder for namespacing (default is disk_cache)
from mysiar.disk_cache_data import disk_cache_data

@disk_cache_data(ttl="30s")
def load_data(a, b):
    return a + b

# first call is whole function process
result = load_data(1, 2)
# each next call within ttl is cached
result = load_data(1, 2)

Clearing the cache of one function

Each decorated function stores its entries in its own subdirectory of the namespace, so a function can drop its own cache without touching any other function.

# drop every cached entry of load_data in the current namespace
load_data.clear()

# drop only the entry cached for these arguments
load_data.clear(1, 2)

# drop the entries of load_data in every namespace under DISK_CACHE_DIR
load_data.clear_all_namespaces()

# path of the directory holding the entries of load_data
path = load_data.cache_dir()

disk_cache_cleanup

Function to cleanup expired or corrupt cache files across all namespaces. It also removes emptied function directories and leftovers of interrupted clear() calls. Entries cached with ttl=None never expire, so cleanup leaves them alone - drop them with clear().

An entry is a pair of files, <key>.pkl and <key>.meta. A crash between the two writes leaves one file without its pair, and cleanup removes it once it is older than 60 seconds. The grace period keeps a writer that is still creating the pair from losing it, and it can be changed per call.

from mysiar.disk_cache_data import disk_cache_cleanup

disk_cache_cleanup()

# drop half written entries immediately instead of after 60 seconds
disk_cache_cleanup(orphan_grace_seconds=0)

Download files

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

Source Distribution

mysiar_disk_cache_data-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mysiar_disk_cache_data-0.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file mysiar_disk_cache_data-0.1.0.tar.gz.

File metadata

  • Download URL: mysiar_disk_cache_data-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for mysiar_disk_cache_data-0.1.0.tar.gz
Algorithm Hash digest
SHA256 575474d1a87e1f7207458f0b0824147975ad576fe53bf08ac61a9020778eab6a
MD5 b36de2aa802d6815041b7962e96848a1
BLAKE2b-256 57a5880330f40e8337cd9817da4b30abe1d2e88b531cc5e74ff07afe628fdbbc

See more details on using hashes here.

File details

Details for the file mysiar_disk_cache_data-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mysiar_disk_cache_data-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05fe385b2dd523448ff2d408f87050be20d305516dc4189d659c864a0de0fb94
MD5 358f1229b64c62b00e9d208e679465e7
BLAKE2b-256 b6c2029162c3b74120f20ef6866ea199a6c1bafa38f16014458c6ff3b97c2d55

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

2 files

0.2.0

2 files

This release

0.1.0 This release

2 files

0.0.1

2 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