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

Static type checking

Run pyright over the entire project:

    python3 -m scripts.check

Testing

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

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

You can also run all tests via the run_tests executable module:

    python3 -m scripts.run_tests

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.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.5.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genericache-0.5.1.tar.gz
Algorithm Hash digest
SHA256 bda3a7683899904148de63cebbc00ea890b534feeba1b871cbc905baf460e0aa
MD5 00512eac27ac9241b6795b9170086ad8
BLAKE2b-256 bcd39f9eb25bcabcd33f8f2129333b8df5e7a9b9b43b9b4ca14c2f5b67df330e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genericache-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5adb91fe956bc802206bcc09e6e3e6e3baa89a721d619e4ab585d6b2a1b405c
MD5 f630c798ca151555ff22d451e7f1fa29
BLAKE2b-256 72dc70c28a36aa68ecaf6945cc7bca868d078dca1279fab9d6906ee8ea483406

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