Skip to main content

A thread-safe, process-safe cache for slow data fetching operations

Project description

Genericache

A thread-safe, process-safe cache for slow fetching operations, like web requests.

Usage

    from genericache import DiskCache
    from genericache.digest import UrlDigest
    from pathlib import Path
    from typing import Iterable
    from hashlib import sha256

    def my_fetch(url: str) -> Iterable[bytes]:
        import httpx
        return httpx.get(url).raise_for_status().iter_bytes(4096)

    def url_hasher(url: str) -> UrlDigest:
        return UrlDigest.from_str(url)

    cache = DiskCache[str].create(
        url_type=str,
        cache_dir=Path("/tmp/my_cache"),
        url_hasher=url_hasher,
    )

    reader, contents_digest = cache.fetch(
        "https://www.ilastik.org/documentation/pixelclassification/snapshots/training2.png",
        fetcher=my_fetch,
    )
    assert sha256(reader.read()).digest() == contents_digest.digest
    assert cache.hits() == 0
    assert cache.misses() == 1

    reader, contents_digest = cache.fetch(
        "https://www.ilastik.org/documentation/pixelclassification/snapshots/training2.png",
        fetcher=my_fetch,
    )
    assert cache.hits() == 1
    assert cache.misses() == 1

Testing

Run the modules inside tests/, e.g.:

uv run --python 3.11 python -m tests.test_disk_cache

You can run them all with something like

    find tests/ -name "__main__.py" | \
    tr '/' '.' | sed 's@\.__main__.py@@' | \
    xargs -t -I test_mod_name \
        uv run --python 3.12 python -m test_mod_name

Project details


Download files

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

Source Distribution

genericache-0.4.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

genericache-0.4.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file genericache-0.4.1.tar.gz.

File metadata

  • Download URL: genericache-0.4.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.5

File hashes

Hashes for genericache-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7b62d29cf939bfe41539a42b01f6b2313b2e33b28af4c837a0864f27331bb875
MD5 e8ecbf229146ab21753d31f0d06a450f
BLAKE2b-256 c8df01a5bc039164a9aba5c6606b89e8e1a652223c8f6fbe1de42fb32b07fc17

See more details on using hashes here.

File details

Details for the file genericache-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for genericache-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3187fe78cfb0a7441175b982c9387d9796301db78ec98c3581ce4cc6efe151f8
MD5 14411ee6339bf898732dd67716eef1d4
BLAKE2b-256 d1aa7dfeadd7db602a76154e00e1a155efe4131c3d8e89d6ff3c8b3d92f8c174

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page