wave_cache
An encrypted, wave-scoped on-disk cache of decoded scan records.
A WaveDecodedCache lives for one wave of an inference run. Records are stored in a
private directory under a disk budget, encrypted with keys that exist only in this
process's memory, and deleted when the cache closes. A directory that outlives its
process (SIGKILL, OOM, power loss) is unreadable because its keys died with the
process, and is removed by sweep_orphaned_wave_caches.
Module
Functions
active_wave_cache
def active_wave_cache() ‑> WaveDecodedCache | None:Return the wave's cache when both scopes are set and it is still open.
derive_wave_cache_keys
def derive_wave_cache_keys( master: bytes, cache_id: bytes,) ‑> WaveCacheKeys:Derive a cache's subkeys from its master key with HKDF-SHA256.
Arguments
master: The 32-byte master key.cache_id: The cache's identifier, used as the HKDF salt.
Returns The subkeys.
Raises
ValueError: Ifmasteris not 32 bytes.
open_wave_cache
def open_wave_cache( *, owner: object, root: Path, location: "Literal['temp', 'custom']", max_bytes: int, min_free_bytes: int, key_source: WaveCacheKeySource | None = None,) ‑> WaveDecodedCache | WaveCacheReport:Open a cache in a new private directory under root; never raises.
Orphaned directories under root are swept first. The budget is the smaller of
max_bytes and half the space above min_free_bytes.
Arguments
owner: The object the cache is opened for; seeWaveDecodedCache.is_bound_to.root: The directory that holds cache directories.location: Whetherrootis the default temp location or a custom one.max_bytes: The most bytes the cache may hold.min_free_bytes: The free space to leave on the volume.key_source: Where the master key comes from; defaults to an in-memory one.
Returns
The open cache, or a WaveCacheReport with outcome unavailable if it could
not be opened (including when the budget is below MIN_BUDGET_BYTES).
resolve_wave_cache_root
def resolve_wave_cache_root() ‑> tuple[pathlib.Path, typing.Literal['temp', 'custom']]:Return the directory that holds wave caches and where it came from.
This is settings.background_wave_cache_dir (custom) when set, otherwise a
bitfount-wave-cache directory under the system temporary directory (temp).
The settings are read at call time, because a live reconfigure replaces the
settings object. Nothing is created or touched on disk.
start_wave_cache_sweep_thread
def start_wave_cache_sweep_thread() ‑> threading.Thread | None:Start a daemon thread that sweeps orphaned wave caches; never raises.
Returns
The started thread, or None when settings.background_wave_cache_enabled
is false (nothing is touched on disk) or the thread could not be started.
sweep_orphaned_wave_caches
def sweep_orphaned_wave_caches(root: Path) ‑> OrphanSweep:Delete cache directories under root whose owning process is gone.
A directory belongs to a live process when the pid in its name is alive with a create time within one second of the one in the name. A directory made by this process is an orphan unless it is one of its open caches. Entries whose names do not match, and non-directories, are never touched. A root that is not exclusively this user's (POSIX) is not swept. Never raises.
wave_cache_consumer
def wave_cache_consumer() ‑> Iterator[None]:Mark this context as one that may use the wave's cache.
wave_cache_key
def wave_cache_key( filename: str, stat: os.stat_result, fingerprint: tuple[str, ...],) ‑> WaveCacheKey:Build the cache key for a file as it currently is on disk.
Arguments
filename: The file's path.stat: The file'sos.stat_result.fingerprint: The options that affect how the file is decoded.
wave_cache_scope
def wave_cache_scope(cache: WaveDecodedCache | None) ‑> Iterator[None]:Make cache the wave's cache within this context.
Classes
CacheLookup
class CacheLookup( records: list[dict[str, Any]] | None, error: bool, seconds: float, bytes_read: int,):The outcome of WaveDecodedCache.get.
Attributes
records: The decoded rows, orNoneon a miss.error: Whether the lookup failed (the entry is then discarded).seconds: The time the lookup took.bytes_read: The size of the entry read; 0 on a miss.
Variables
-
bytes_read : int
-
error : bool
-
records : list[dict[str, typing.Any]] | None
-
seconds : float
CacheStore
class CacheStore( stored: bool, reason: NotStoredReason | None, error: bool, seconds: float, bytes_written: int,):The outcome of WaveDecodedCache.put.
Attributes
stored: Whether the records were stored.reason: Why they were not, orNoneif they were.error: Whether the store failed.seconds: The time the store took.bytes_written: The size of the entry written; 0 if not stored.
Variables
-
bytes_written : int
-
error : bool
-
reason : NotStoredReason | None
-
seconds : float
-
stored : bool
EphemeralKeySource
class EphemeralKeySource():A key source whose master key is random and held in memory only.
Generate the master key.
Methods
cache_master_key
def cache_master_key(self, cache_id: bytes) ‑> bytes:Return the master key; it is the same for every cache_id.
OrphanSweep
class OrphanSweep(deleted: int, failed: int, bytes_reclaimed: int):The outcome of a sweep for orphaned cache directories.
Attributes
deleted: The directories removed.failed: The directories that could not be removed.bytes_reclaimed: The bytes of file content in the removed directories.
WaveCacheKeySource
class WaveCacheKeySource(*args, **kwargs):A source of the 32-byte master key a cache derives its subkeys from.
Ancestors
Methods
cache_master_key
def cache_master_key(self, cache_id: bytes) ‑> bytes:Return the 32-byte master key for the cache identified by cache_id.
WaveCacheKeys
class WaveCacheKeys(siv_key: bytes, siv_nonce: bytes, gcm_key: bytes):The subkeys of one cache.
Attributes
siv_key: The 64-byte AES-SIV key that encrypts entry keys.siv_nonce: The 16 bytes of associated data for AES-SIV.gcm_key: The 32-byte AES-256-GCM key that encrypts record values.
WaveCacheReport
class WaveCacheReport( outcome: "Literal['closed', 'delete_failed', 'unavailable']", reason: str | None, records_stored: int, bytes_stored: int, hits: int, misses: int, admission_stopped: AdmissionStop | None, budget_bytes: int | None, free_bytes_at_open: int | None, lifetime_seconds: float | None, cleanup_seconds: float | None, orphans: OrphanSweep, location: "Literal['temp', 'custom']",):What a cache did over its lifetime, or why it never opened.
Attributes
outcome:closed,delete_failed(the directory outlived the close) orunavailable(the cache never opened).reason: Why the cache is unavailable, otherwiseNone.records_stored: The entries stored.bytes_stored: The bytes written for them.hits: The lookups that returned records.misses: The lookups that did not.admission_stopped: Why storing stopped, if it did.budget_bytes: The disk budget, if one was computed.free_bytes_at_open: The free space on the volume when the cache opened.lifetime_seconds: The time between open and close.cleanup_seconds: The time the close took to delete the directory.orphans: The sweep that ran before the directory was created.location: Whether the cache root is the default temp location or custom.
Variables
-
admission_stopped : AdmissionStop | None
-
budget_bytes : int | None
-
bytes_stored : int
-
cleanup_seconds : float | None
-
free_bytes_at_open : int | None
-
hits : int
-
lifetime_seconds : float | None
-
location : Literal['temp', 'custom']
-
misses : int
-
orphans : OrphanSweep
-
outcome : Literal['closed', 'delete_failed', 'unavailable']
-
reason : str | None
-
records_stored : int
WaveDecodedCache
class WaveDecodedCache( *, owner: object, directory: Path, db: diskcache.Cache, disk: _EncryptedRecordDisk, keys: WaveCacheKeys, key_source: WaveCacheKeySource, budget_bytes: int, min_free_bytes: int, free_bytes_at_open: int, location: "Literal['temp', 'custom']", orphans: OrphanSweep,):An encrypted on-disk cache of decoded records for one wave.
Built only by open_wave_cache. get, put and close share one lock; after
close, every lookup is a miss and every store is a no-op.
Wrap an already opened and bound diskcache.Cache.
Variables
budget_bytes : int- The most bytes the cache admits.
closed : bool- Whether the cache has been closed.
directory : Path- The cache's private directory.
Methods
close
def close(self) ‑> WaveCacheReport:Close the cache, drop its keys and delete its directory.
Idempotent: later calls return the first call's report. Never raises.
get
def get(self, key: WaveCacheKey) ‑> CacheLookup:Look up the records stored under key.
A missing entry is a miss. A tampered or undecodable entry is a miss that is also deleted and flagged as an error. A value file that cannot be opened is a plain miss: diskcache treats it as absent and the entry is left in place.
is_bound_to
def is_bound_to(self, owner: object) ‑> bool:Return whether this cache was opened for owner (by identity).
put
def put( self, key: WaveCacheKey, records: list[dict[str, Any]],) ‑> CacheStore:Store records under key if the budget and free-space floor allow.