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.3.0.tar.gz (7.8 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.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genericache-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5099d03b7c501f7844c862209ef7f19cc5759577f806fd415cc2bc6518bddc1d
MD5 a58bc4566bc17d006dc62447f3944033
BLAKE2b-256 43583a5b0c98097602d2012fc91527f6ad0186a20a9a8ebb7b771ff7240643c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genericache-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e0e8596df331785c32d6c9dfe3009df6e74a248a57ca78090118c362a8b2a84
MD5 35381ccb142bc8cc68ac4d5d6aa01c16
BLAKE2b-256 0cf0650b7656216bfc43b903e378f7e85c654e2c103aa2c013f400b5235a81c3

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