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.5.0.tar.gz (8.2 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.5.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genericache-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e6d6ec1ab44826899d5ce39895922440e6c1784ccefeada35f908936f25b5483
MD5 1c00c56c69d55cfafba11f0f9fe87868
BLAKE2b-256 9c355ce0a5db24d1ff2ac6a999f63044e4ded4ed16c85c92605ccda6e3683f32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genericache-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17a0d1d652cdcf1dacd506a16bc997112be1db0e4a320aea4d5c3bcea2aa4559
MD5 44f1229ca46cccce1b09df4e0969b166
BLAKE2b-256 d34164ae1ea7d4ef77ed7e5fe374990ec82017a1aa917ef9891693e762c22b6b

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