pub struct LruHandle<K: LruKey, T: LruValue> {
next_hash: *mut LruHandle<K, T>,
next: *mut LruHandle<K, T>,
prev: *mut LruHandle<K, T>,
kv: Option<(K, T)>,
hash: u64,
charge: usize,
refs: u32,
flags: u8,
}
Expand description
An entry is a variable length heap-allocated structure. Entries are referenced by cache and/or by any external entity. The cache keeps all its entries in a hash table. Some elements are also stored on LRU list.
LruHandle
can be in these states:
- Referenced externally AND in hash table.
In that case the entry is not in the LRU list
(
refs
>= 1 &&in_cache
== true) - Not referenced externally AND in hash table.
In that case the entry is in the LRU list and can be freed.
(
refs
== 0 &&in_cache
== true) - Referenced externally AND not in hash table.
In that case the entry is not in the LRU list and not in hash table.
The entry can be freed when refs becomes 0.
(
refs
>= 1 &&in_cache
== false)
All newly created LruHandle
s are in state 1. If you call
LruCacheShard::release
on entry in state 1, it will go into state 2.
To move from state 1 to state 3, either call LruCacheShard::erase
or
LruCacheShard::insert
with the same key (but possibly different value).
To move from state 2 to state 1, use LruCacheShard::lookup
.
Before destruction, make sure that no handles are in state 1. This means
that any successful LruCacheShard::lookup/LruCacheShard::insert
have a
matching LruCache::release
(to move into state 2) or LruCacheShard::erase
(to move into state 3).
Fields§
§next_hash: *mut LruHandle<K, T>
next element in the linked-list of hash bucket, only used by hash-table.
next: *mut LruHandle<K, T>
next element in LRU linked list
prev: *mut LruHandle<K, T>
prev element in LRU linked list
kv: Option<(K, T)>
When the handle is on-use, the fields is Some(...)
, while the handle is cleared up and
recycled, the field is None
.
hash: u64
§charge: usize
§refs: u32
The count for external references. If refs > 0
, the handle is not in the lru cache, and
when refs == 0
, the handle must either be in LRU cache or has been recycled.
flags: u8
Implementations§
source§impl<K: LruKey, T: LruValue> LruHandle<K, T>
impl<K: LruKey, T: LruValue> LruHandle<K, T>
pub fn new(key: K, value: T, hash: u64, charge: usize) -> Self
pub fn init(&mut self, key: K, value: T, hash: u64, charge: usize)
sourcefn set_in_cache(&mut self, in_cache: bool)
fn set_in_cache(&mut self, in_cache: bool)
Set the in_cache
bit in the flag
Since only in_cache
reflects whether the handle is present in the hash table, this method
should only be called in the method of hash table. Whenever the handle enters the hash
table, we should call set_in_cache(true)
, and whenever the handle leaves the hash table,
we should call set_in_cache(false)
fn is_high_priority(&self) -> bool
fn set_high_priority(&mut self, high_priority: bool)
fn set_in_high_pri_pool(&mut self, in_high_pri_pool: bool)
fn is_in_high_pri_pool(&self) -> bool
fn add_ref(&mut self)
fn add_multi_refs(&mut self, ref_count: u32)
fn unref(&mut self) -> bool
fn has_refs(&self) -> bool
sourcefn is_in_cache(&self) -> bool
fn is_in_cache(&self) -> bool
Test whether the handle is in cache. in cache
is equivalent to that the handle is in the
hash table.
unsafe fn get_key(&self) -> &K
unsafe fn get_value(&self) -> &T
unsafe fn is_same_key(&self, key: &K) -> bool
unsafe fn take_kv(&mut self) -> (K, T)
fn is_in_lru(&self) -> bool
fn set_in_lru(&mut self, in_lru: bool)
Trait Implementations§
impl<K: LruKey, T: LruValue> Send for LruHandle<K, T>
Auto Trait Implementations§
impl<K, T> Freeze for LruHandle<K, T>
impl<K, T> RefUnwindSafe for LruHandle<K, T>where
K: RefUnwindSafe,
T: RefUnwindSafe,
impl<K, T> !Sync for LruHandle<K, T>
impl<K, T> Unpin for LruHandle<K, T>
impl<K, T> UnwindSafe for LruHandle<K, 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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<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 moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
source§impl<M> MetricVecRelabelExt for M
impl<M> MetricVecRelabelExt for M
source§fn relabel(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level
.source§fn relabel_n(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
relabel_num: usize,
) -> RelabeledMetricVec<M>
fn relabel_n( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, relabel_num: usize, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
.source§fn relabel_debug_1(
self,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel_debug_1( self, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
with metric_level
set to
MetricLevel::Debug
and relabel_num
set to 1.