pub struct FileParseCache<T, F: Fingerprint = MtimeFingerprint> { /* private fields */ }Expand description
Mtime-gated (or content-hash-gated) file parse cache.
On get, the fingerprint of the file is checked. If it matches the cached
stamp, the cached T is returned without re-parsing. On mismatch or cache
miss the parser closure runs and the result is stored.
Backed by moka::sync::Cache with bounded-size LRU eviction.
Implementations§
Source§impl<T: Clone + Send + Sync + 'static> FileParseCache<T, MtimeFingerprint>
impl<T: Clone + Send + Sync + 'static> FileParseCache<T, MtimeFingerprint>
Source§impl<T, F> FileParseCache<T, F>
impl<T, F> FileParseCache<T, F>
Sourcepub fn with_fingerprint(max_entries: u64, fingerprint: F) -> Self
pub fn with_fingerprint(max_entries: u64, fingerprint: F) -> Self
Create a cache with a custom fingerprint strategy.
Sourcepub fn get<E>(
&self,
path: &Path,
parser: impl FnOnce(&Path) -> Result<T, E>,
) -> Result<T, E>
pub fn get<E>( &self, path: &Path, parser: impl FnOnce(&Path) -> Result<T, E>, ) -> Result<T, E>
Return the cached value for path, or parse it via parser on miss /
fingerprint change.
If multiple threads call get for the same path concurrently and all
miss, each thread runs parser independently. The last writer wins in
moka; all callers receive a correct (freshly-parsed) value. This trades
a rare redundant parse for a simpler API — coalescing via
try_get_with would require wrapping the caller’s error in Arc.
On unreadable files (missing, permissions), the fingerprint stat fails
and returns Err(E::from(io::Error)) without invoking parser.
Sourcepub fn purge_if(&self, predicate: impl Fn(&Path) -> bool)
pub fn purge_if(&self, predicate: impl Fn(&Path) -> bool)
Remove entries where predicate returns true.
Not atomic: takes a snapshot via iteration, then invalidates matching
keys one by one. An entry inserted by a concurrent get between the
snapshot and the invalidation pass will be missed — it will be caught
on the next purge_if call. Invalidated entries become immediately
invisible to get, but len() reflects the removal only after its
own pending-task flush.
Allocates O(cache size) for the key snapshot — every key is cloned
into a temporary Vec before invalidation begins. Fine for caches
under ~10K entries. For larger caches, prefer moka’s built-in
TTL/TTI-based eviction over manual purging.
Sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Number of entries currently in the cache.
Flushes pending bookkeeping before reading the count so the value is
immediately consistent with preceding get, purge_if, and clear
calls. Cost is O(pending operations), not O(1) — typically microseconds
for caches with low write rates, but callers in tight loops should be
aware.