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(name: &'static str, size: usize, cleanup_interval_secs: u64) -> Self
pub fn new(name: &'static str, size: usize, cleanup_interval_secs: u64) -> Self
Creates a new ValueCache with the given instance name (used as the
cache metric label), bounded-cache capacity, and cleanup interval
for the weak-reference index.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of entries currently held in the bounded cache.
This is the live occupancy (excludes the weak dedup index), and is
periodically sampled as the value_cache_entries gauge for the
occupancy-vs-capacity diagnosis.
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.
Sourcepub fn get(&self, key: &K) -> Option<Arc<V>>
pub fn get(&self, key: &K) -> Option<Arc<V>>
Returns an crate::Arc to the value, checking both the bounded
cache and the weak index.
Sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Returns true if the value exists in either the bounded cache or
the weak index (with a live allocation).
Sourcepub fn cleanup_dead_entries(&self)
pub fn cleanup_dead_entries(&self)
Removes all dead Weak entries from the weak index.
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>
Sourcepub fn insert_all_hashed<'a, T>(
&self,
values: impl IntoIterator<Item = Cow<'a, Hashed<T>>>,
)
pub fn insert_all_hashed<'a, T>( &self, values: impl IntoIterator<Item = Cow<'a, Hashed<T>>>, )
Inserts multiple values constructed from Hashed<T>s into the cache.
Auto Trait Implementations§
impl<K, V> Freeze for ValueCache<K, V>
impl<K, V> !RefUnwindSafe 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>
impl<K, V> UnsafeUnpin for ValueCache<K, V>
impl<K, V> !UnwindSafe for ValueCache<K, V>
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T> MockResults for T
impl<T> MockResults for T
Source§type Results = T
type Results = T
MockInstance.Source§impl<T> Pointable for T
impl<T> Pointable for T
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.