Skip to main content

FileParseCache

Struct FileParseCache 

Source
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>

Source

pub fn new(max_entries: u64) -> Self

Create a cache with mtime-based invalidation and room for max_entries files.

Source§

impl<T, F> FileParseCache<T, F>
where T: Clone + Send + Sync + 'static, F: Fingerprint,

Source

pub fn with_fingerprint(max_entries: u64, fingerprint: F) -> Self

Create a cache with a custom fingerprint strategy.

Source

pub fn get<E>( &self, path: &Path, parser: impl FnOnce(&Path) -> Result<T, E>, ) -> Result<T, E>
where E: From<Error> + Send + Sync + 'static,

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.

Source

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.

Source

pub fn clear(&self)

Remove all entries.

Source

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.

Source

pub fn is_empty(&self) -> bool

Whether the cache is empty.

Same consistency and cost as len.

Trait Implementations§

Source§

impl<T, F> Debug for FileParseCache<T, F>
where T: Clone + Send + Sync + 'static + Debug, F: Fingerprint + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, F = MtimeFingerprint> !Freeze for FileParseCache<T, F>

§

impl<T, F = MtimeFingerprint> !RefUnwindSafe for FileParseCache<T, F>

§

impl<T, F> Send for FileParseCache<T, F>
where T: Send + Sync,

§

impl<T, F> Sync for FileParseCache<T, F>
where T: Send + Sync,

§

impl<T, F> Unpin for FileParseCache<T, F>

§

impl<T, F> UnsafeUnpin for FileParseCache<T, F>

§

impl<T, F = MtimeFingerprint> !UnwindSafe for FileParseCache<T, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.