pub struct BackendCache<T: Send + 'static> { /* private fields */ }Expand description
LRU-bounded cache of loaded inference backends.
Not itself Sync over T — the cache stores handles only, and the
lock guarding the map is coarse (held only for look-up / insert /
evict). Per-entry mutation is serialized by each entry’s own Mutex.
Implementations§
Source§impl<T: Send + 'static> BackendCache<T>
impl<T: Send + 'static> BackendCache<T>
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Read the CAR_INFERENCE_MODEL_CACHE_MB env var, default 24 GB.
A value of 0 disables caching (every call loads fresh).
Sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
Is this a disabled (zero-budget) cache?
Sourcepub fn get_or_load<E>(
&self,
key: &str,
size_bytes: u64,
loader: impl FnOnce() -> Result<T, E>,
) -> Result<CachedBackend<T>, E>
pub fn get_or_load<E>( &self, key: &str, size_bytes: u64, loader: impl FnOnce() -> Result<T, E>, ) -> Result<CachedBackend<T>, E>
Get a handle to the cached backend, or load + insert it.
loader is invoked only on a cache miss. If the new entry plus
existing entries exceed the budget, the oldest entries are
evicted until we’re within budget (or only the new entry remains).
size_bytes should be an approximate on-disk or in-memory size
for the loaded backend; estimate_model_size is a reasonable
default that sums the .safetensors file sizes in a model dir.
Sourcepub fn invalidate(&self, key: &str)
pub fn invalidate(&self, key: &str)
Manually remove a key, e.g. when model weights have been updated
on disk. Outstanding Arc<Mutex<T>> handles remain valid.
Auto Trait Implementations§
impl<T> !Freeze for BackendCache<T>
impl<T> RefUnwindSafe for BackendCache<T>
impl<T> Send for BackendCache<T>
impl<T> Sync for BackendCache<T>
impl<T> Unpin for BackendCache<T>
impl<T> UnsafeUnpin for BackendCache<T>
impl<T> UnwindSafe for BackendCache<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