pub struct Cache { /* private fields */ }Expand description
The cache: a directory tree of one JSON file per entry, keyed by blake3 digest of the six prompt and backend inputs.
Built once per process and shared across the analyzer; every method takes
&self so Cache can live behind an Arc if a future caller needs it.
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn new(root: PathBuf, ttl_days: u64, max_bytes: u64) -> Self
pub fn new(root: PathBuf, ttl_days: u64, max_bytes: u64) -> Self
Build a cache rooted at root without touching the filesystem.
put creates its shard on demand. Keeping construction lazy lets site
policy decide whether a semantic layer exists before that layer acquires
any on-disk state.
Sourcepub fn default_root() -> PathBuf
pub fn default_root() -> PathBuf
The conventional location: directories::ProjectDirs::cache_dir(),
falling back to .drep-cache in the cwd when the platform has no
cache directory. Returned as a relative path on the fallback branch
so the caller can resolve it against whatever cwd they choose.
Sourcepub fn key(
&self,
system_prompt: &str,
content: &str,
backend: &str,
model: &str,
request_identity: &str,
temperature: Option<f32>,
) -> CacheKey
pub fn key( &self, system_prompt: &str, content: &str, backend: &str, model: &str, request_identity: &str, temperature: Option<f32>, ) -> CacheKey
Compute the key for
(system_prompt, content, backend, model, request_identity, temperature).
Deliberately does NOT consult self: the key is content-only, so two
Cache instances at different roots produce the same key for the
same inputs. That is what criterion 7 asserts and what makes the
cache portable across CI runs.
request_identity is in the key because one endpoint can serve the same
model through different protocol, token-ceiling and header-selected
routes. Keying without it files one request’s answer where another looks
for its own, which is the same defect that put endpoint in the key.
Sourcepub fn get(&self, key: &CacheKey) -> Option<Value>
pub fn get(&self, key: &CacheKey) -> Option<Value>
Read the entry at key, or None if absent, expired, unreadable, or
unparseable. Reads never fail.
An expired entry is removed opportunistically; a corrupt or unreadable entry is left in place so the next read has another chance (transient I/O is more likely than persistent corruption, and a half-deleted cache is worse than a slightly redundant one).
Sourcepub fn put(&self, key: &CacheKey, value: &Value) -> Result<(), CacheError>
pub fn put(&self, key: &CacheKey, value: &Value) -> Result<(), CacheError>
Write value to disk under key.
Creates the shard directory on demand so the very first put into a
fresh cache does not need a separate bootstrap step.
Sourcepub fn evict_if_needed(&self) -> Result<u64, CacheError>
pub fn evict_if_needed(&self) -> Result<u64, CacheError>
Walk the tree, evicting the oldest entries (by mtime) until the
total size is at or under max_bytes. Returns the number of bytes
freed; 0 when no eviction was needed.