pub struct AdaptiveCache<V: Clone + Send + Sync + 'static> { /* private fields */ }Expand description
Adaptive LRU cache with intelligent TTL adjustment
This cache monitors access patterns and adjusts TTL dynamically:
- Frequently accessed items get longer TTL
- Rarely accessed items get shorter TTL
- Reduces memory waste on cold items
- Improves hit rate for hot items
§Thread Safety
This cache is designed for single-threaded async access. All public methods acquire the write lock internally. For higher concurrency, consider using fine-grained locking strategies.
Implementations§
Source§impl<V: Clone + Send + Sync + 'static> AdaptiveCache<V>
impl<V: Clone + Send + Sync + 'static> AdaptiveCache<V>
Sourcepub fn new(config: AdaptiveCacheConfig) -> Self
pub fn new(config: AdaptiveCacheConfig) -> Self
Create a new adaptive cache with default configuration
Sourcepub async fn get(&self, id: Uuid) -> Option<V>
pub async fn get(&self, id: Uuid) -> Option<V>
Get the current value for an entry without recording access
Sourcepub async fn get_and_record(&self, id: Uuid) -> Option<V>
pub async fn get_and_record(&self, id: Uuid) -> Option<V>
Get the current value and record access
Sourcepub async fn access_count(&self, id: Uuid) -> Option<usize>
pub async fn access_count(&self, id: Uuid) -> Option<usize>
Get access count for an entry
Sourcepub async fn get_metrics(&self) -> AdaptiveCacheMetrics
pub async fn get_metrics(&self) -> AdaptiveCacheMetrics
Get current cache metrics
Sourcepub async fn cleanup_expired(&self) -> usize
pub async fn cleanup_expired(&self) -> usize
Manually cleanup expired entries
Sourcepub async fn cold_count(&self) -> usize
pub async fn cold_count(&self) -> usize
Get the number of cold items
Sourcepub fn stop_cleanup(&mut self)
pub fn stop_cleanup(&mut self)
Stop the background cleanup task
Trait Implementations§
Auto Trait Implementations§
impl<V> Freeze for AdaptiveCache<V>
impl<V> !RefUnwindSafe for AdaptiveCache<V>
impl<V> Send for AdaptiveCache<V>
impl<V> Sync for AdaptiveCache<V>
impl<V> Unpin for AdaptiveCache<V>
impl<V> UnsafeUnpin for AdaptiveCache<V>
impl<V> !UnwindSafe for AdaptiveCache<V>
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
Mutably borrows from an owned value. Read more
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>
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 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>
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 moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.