pub struct ValueCache<K, V> { /* private fields */ }Expand description
A concurrent cache with efficient eviction and single-allocation guarantees.
Backed by quick_cache (S3-FIFO eviction) for bounded hot-path caching, plus
a lock-free papaya::HashMap weak index for deduplication. Together they
guarantee that at most one Arc<V> allocation exists per key at any time.
A background task periodically sweeps dead Weak entries from the index
to prevent unbounded memory growth.
Implementations§
Source§impl<K, V> ValueCache<K, V>
impl<K, V> ValueCache<K, V>
Sourcepub fn new(size: usize, cleanup_interval_secs: u64) -> Self
pub fn new(size: usize, cleanup_interval_secs: u64) -> Self
Creates a new ValueCache with the given bounded-cache capacity
and cleanup interval for the weak-reference index.
Sourcepub fn insert(&self, key: &K, value: V) -> Arc<V>
pub fn insert(&self, key: &K, value: V) -> Arc<V>
Inserts a value into the cache, returning the canonical crate::Arc.
The value is wrapped in Arc internally. If a live Arc for this key
already exists (held by another consumer), the existing allocation is
reused and the new value is dropped.
Sourcepub fn remove(&self, key: &K) -> Option<Arc<V>>
pub fn remove(&self, key: &K) -> Option<Arc<V>>
Removes a value from the bounded cache.
The weak index entry is intentionally kept — another consumer may
still hold an Arc to this value, and the weak index must be able
to deduplicate against it. Dead weak entries are cleaned up by the
background task.
Source§impl<V: Clone + Send + Sync + 'static> ValueCache<CryptoHash, V>
impl<V: Clone + Send + Sync + 'static> ValueCache<CryptoHash, V>
Sourcepub fn insert_hashed<T>(&self, value: Cow<'_, Hashed<T>>) -> Arc<V>
pub fn insert_hashed<T>(&self, value: Cow<'_, Hashed<T>>) -> Arc<V>
Auto Trait Implementations§
impl<K, V> !RefUnwindSafe for ValueCache<K, V>
impl<K, V> !UnwindSafe for ValueCache<K, V>
impl<K, V> Freeze for ValueCache<K, V>
impl<K, V> Send for ValueCache<K, V>
impl<K, V> Sync for ValueCache<K, V>
impl<K, V> Unpin for ValueCache<K, V>where
K: Unpin,
impl<K, V> UnsafeUnpin for ValueCache<K, V>
Blanket Implementations§
impl<_INNER> AutoTraits for _INNER
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<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreimpl<T> MaybeSend for Twhere
T: Send,
impl<T> MaybeSync for Twhere
T: Sync,
Source§impl<M, I> RuntimeMemory<&mut I> for Mwhere
M: RuntimeMemory<I>,
impl<M, I> RuntimeMemory<&mut I> for Mwhere
M: RuntimeMemory<I>,
Source§fn read<'instance>(
&self,
instance: &'instance &mut I,
location: GuestPointer,
length: u32,
) -> Result<Cow<'instance, [u8]>, RuntimeError>
fn read<'instance>( &self, instance: &'instance &mut I, location: GuestPointer, length: u32, ) -> Result<Cow<'instance, [u8]>, RuntimeError>
Reads length bytes from memory from the provided location.
Source§fn write(
&mut self,
instance: &mut &mut I,
location: GuestPointer,
bytes: &[u8],
) -> Result<(), RuntimeError>
fn write( &mut self, instance: &mut &mut I, location: GuestPointer, bytes: &[u8], ) -> Result<(), RuntimeError>
Writes the bytes to memory at the provided location.