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.2.tar.gz (8.3 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.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genericache-0.5.2.tar.gz
Algorithm Hash digest
SHA256 1bdc21b0471ffd3092c78dbbee2c757ad444baa746073c68068f0f41509d826f
MD5 0f64411ea7b10230f7ab79607e1de545
BLAKE2b-256 018b9773e74ece19b9fbf1d0f6d78f659091dfb8c4549107e9d95b15b6e8ff5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genericache-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c922cf07df74280911d79068f43583f86b4d6caee1704d79c779d9fd6547a72
MD5 7b34face1639c35728f2af7c2d3e4fd7
BLAKE2b-256 62a13f982f9a8f082760b6970ad6c25573bb2526dc180b695f0f50fa3f392d60

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