pub struct JsonCache<T> { /* private fields */ }Expand description
A generic file-based cache with atomic writes, TTL, and schema versioning.
Each entry is a JSON file under cache_dir whose name derives from the
entry key. Writes go to a temporary sibling file that is then renamed over
the target, so a reader never observes a torn write. Reads enforce both the
configured TTL (via file mtime) and the CACHE_SCHEMA_VERSION envelope.
Implementations§
Source§impl<T> JsonCache<T>where
T: Serialize + DeserializeOwned,
impl<T> JsonCache<T>where
T: Serialize + DeserializeOwned,
Sourcepub fn new(cache_dir: PathBuf, ttl: Duration) -> Result<Self>
pub fn new(cache_dir: PathBuf, ttl: Duration) -> Result<Self>
Create a cache rooted at cache_dir, creating the directory if needed.
Sourcepub fn path_for(&self, file_name: &str) -> PathBuf
pub fn path_for(&self, file_name: &str) -> PathBuf
Path for a named entry (the name must already be filesystem-safe).
Sourcepub fn get_named(&self, file_name: &str) -> Option<T>
pub fn get_named(&self, file_name: &str) -> Option<T>
Read a cached value by file name.
Returns None (evicting the file) when the entry is missing, older
than the TTL, has a mismatched schema version, or fails to parse.
One exception: in process-wide offline mode a TTL-expired entry is served (and not evicted) so an air-gapped run can fall back to whatever data is on disk. A staleness warning is logged. The online path is unchanged — expired entries are still evicted on read.
Sourcepub fn get_named_allow_stale(
&self,
file_name: &str,
) -> Option<(T, Option<Duration>)>
pub fn get_named_allow_stale( &self, file_name: &str, ) -> Option<(T, Option<Duration>)>
Read a cached value, tolerating TTL expiry when offline.
Returns the payload together with a staleness signal: Some(age) is the
amount the entry is past its TTL when it was served despite being
expired (only possible in offline mode), or None when the entry was
still fresh.
Unlike get_named, this never logs; it is the
building block callers use when they want to surface the staleness
(e.g. as CRA evidence) rather than only warn.
Sourcepub fn set_named<V: Serialize + ?Sized>(
&self,
file_name: &str,
value: &V,
) -> Result<()>
pub fn set_named<V: Serialize + ?Sized>( &self, file_name: &str, value: &V, ) -> Result<()>
Atomically write a value under file_name.
Sourcepub fn set<V: Serialize + ?Sized>(
&self,
key: &CacheKey,
value: &V,
) -> Result<()>
pub fn set<V: Serialize + ?Sized>( &self, key: &CacheKey, value: &V, ) -> Result<()>
Atomically write a value keyed by CacheKey.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Aggregate statistics over the cache directory.
Auto Trait Implementations§
impl<T> Freeze for JsonCache<T>
impl<T> RefUnwindSafe for JsonCache<T>
impl<T> Send for JsonCache<T>
impl<T> Sync for JsonCache<T>
impl<T> Unpin for JsonCache<T>
impl<T> UnsafeUnpin for JsonCache<T>
impl<T> UnwindSafe for JsonCache<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more