zea.data.chunk_cache¶
zea.data.chunk_cache¶
On-disk cache of compressed HDF5 chunks fetched over the network.
Streaming re-fetches the same bytes every time: reading the same 5 frames of an hf://
file three times costs the same ~0.9 s three times. The chunks are immutable and we already
fetch at chunk granularity, so they cache exactly.
Keyed by the file’s content hash (HF hands us lfs.sha256 on open, for free) rather
than its URL, because an hf:// resolve URL points at a mutable ref: re-uploading a file
changes what /resolve/main/ returns while the URL stays the same. Keying on content means
a re-upload simply misses, instead of silently serving stale bytes.
Compressed bytes are stored, not decoded arrays: it is the network that costs, and decoding is
fast and parallel (see zea.data.chunk_reader).
Module Attributes
Cache is enabled unless ZEA_CHUNK_CACHE=0. |
|
Byte budget for the whole cache. |
|
Prune this often (in writes) rather than on every one — the check has to stat the tree. |
Functions
|
A cache for the file described by |
|
Remove every cached chunk. |
|
Delete least-recently-used chunks until the cache fits in |
Classes
|
A read-only file object that serves h5py's metadata reads from the chunk cache. |
|
Compressed chunks of one file, on disk, addressed by |
- class zea.data.chunk_cache.CachedFile(fileobj, cache, block_size=None)[source]¶
Bases:
objectA read-only file object that serves h5py’s metadata reads from the chunk cache.
Opening a streamed file re-reads its HDF5 metadata (superblock, group and chunk B-trees) over the network every time — and HF’s CDN intermittently stalls ~3 s on the first range request to a cold object, which is then most of the cost of an open. Those metadata bytes are as cacheable as the chunks are, so they go in the same cache.
Reads are served in aligned blocks, since h5py’s metadata reads are many and small; a block is the unit that gets stored. Chunks and blocks share one keyspace safely: a key is
(offset, size)into an immutable file, so the same key always means the same bytes.block_sizedefaults to the streamed file object’s own block size, so a cached open and an uncached one fetch the same ranges — a smaller block here would split each of the underlying reads into several requests.- property closed: bool¶
- details¶
this is where the chunk fetcher reads the file’s content hash from.
- Type:
Passed through
- class zea.data.chunk_cache.ChunkCache(content_id, root=None)[source]¶
Bases:
objectCompressed chunks of one file, on disk, addressed by
(offset, size).
- zea.data.chunk_cache.ENABLED = True¶
Cache is enabled unless ZEA_CHUNK_CACHE=0.
- zea.data.chunk_cache.MAX_BYTES = 10737418240¶
Byte budget for the whole cache. Overrun is pruned oldest-first (by access time).
- zea.data.chunk_cache.PRUNE_EVERY = 64¶
Prune this often (in writes) rather than on every one — the check has to stat the tree.
- zea.data.chunk_cache.cache_for(details, root=None)[source]¶
A cache for the file described by
details(an fsspecinfomapping).Nonewhen caching is off, or when nothing indetailsidentifies the content. A weaker key (the path, say) would go stale on re-upload, and a stale chunk cache is worse than no chunk cache: it corrupts reads silently.- Return type:
ChunkCache|None