Skip to main content

JsonCache

Struct JsonCache 

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

Source

pub fn new(cache_dir: PathBuf, ttl: Duration) -> Result<Self>

Create a cache rooted at cache_dir, creating the directory if needed.

Source

pub fn path_for(&self, file_name: &str) -> PathBuf

Path for a named entry (the name must already be filesystem-safe).

Source

pub fn dir(&self) -> &Path

Cache directory root.

Source

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.

Source

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.

Source

pub fn set_named<V: Serialize + ?Sized>( &self, file_name: &str, value: &V, ) -> Result<()>

Atomically write a value under file_name.

Source

pub fn get(&self, key: &CacheKey) -> Option<T>

Read a cached value by CacheKey.

Source

pub fn set<V: Serialize + ?Sized>( &self, key: &CacheKey, value: &V, ) -> Result<()>

Atomically write a value keyed by CacheKey.

Source

pub fn remove(&self, key: &CacheKey) -> Result<()>

Remove a cached entry by CacheKey.

Source

pub fn clear(&self) -> Result<()>

Remove every JSON entry from the cache directory.

Source

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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more