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
Release history Release notifications | RSS feed
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.0.tar.gz
(7.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file genericache-0.4.0.tar.gz.
File metadata
- Download URL: genericache-0.4.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3e13277b3e963d57f2d29d3c382f887af0c8c769c1c6394674acff0b1c5a746
|
|
| MD5 |
f5631a0232d133345b698cc239e612e1
|
|
| BLAKE2b-256 |
b7f039632887452cdcb7d2f191ad04c0fa4c95746ffd74d298447c1c8e10c6f7
|
File details
Details for the file genericache-0.4.0-py3-none-any.whl.
File metadata
- Download URL: genericache-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
561fdc2b2abb2bbcc5f95793abcd1cc1677f7819bf15fd188383fe4c265ab3a0
|
|
| MD5 |
1e71e659081536c6178517c21d2883a6
|
|
| BLAKE2b-256 |
93cce02452b5a52cd6e6560663dc376b168f6ce4dcf782dfc851a5de6f81324a
|