Cache

Struct Cache 

Source
pub struct Cache<K, V>
where K: Eq + Hash + Clone,
{ /* private fields */ }
Expand description

LRU 缓存

Implementations§

Source§

impl<K, V> Cache<K, V>
where K: Eq + Hash + Clone,

Source

pub fn new(config: CacheConfig) -> Self

创建新缓存

Source

pub fn with_defaults() -> Self

使用默认配置创建

Source

pub fn insert(&self, key: K, value: V)

插入条目

Source

pub fn insert_with_ttl(&self, key: K, value: V, ttl: Option<Duration>)

插入条目(自定义 TTL)

Source

pub fn get(&self, key: &K) -> Option<V>
where V: Clone,

获取值(如果存在且未过期)

Source

pub fn with_value<R, F>(&self, key: &K, f: F) -> Option<R>
where F: FnOnce(&V) -> R,

获取值的引用(通过闭包访问)

Source

pub fn contains_key(&self, key: &K) -> bool

检查键是否存在(不更新访问时间)

Source

pub fn remove(&self, key: &K) -> Option<V>

删除条目

Source

pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
where V: Clone, F: FnOnce() -> V,

获取或插入

Source

pub fn clear(&self)

清空缓存

Source

pub fn len(&self) -> usize

获取当前大小

Source

pub fn is_empty(&self) -> bool

检查是否为空

Source

pub fn stats(&self) -> CacheStatsSnapshot

获取统计快照

Source

pub fn reset_stats(&self)

重置统计

Source

pub fn cleanup_expired(&self) -> usize

手动清理过期条目

Source

pub fn keys(&self) -> Vec<K>

获取所有键

Source

pub fn insert_many<I>(&self, items: I)
where I: IntoIterator<Item = (K, V)>,

批量插入

Source

pub fn update<F>(&self, key: &K, f: F) -> bool
where F: FnOnce(&mut V),

更新已有条目的值(如果存在)

Trait Implementations§

Source§

impl<K, V> Default for Cache<K, V>
where K: Eq + Hash + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V> !Freeze for Cache<K, V>

§

impl<K, V> !RefUnwindSafe for Cache<K, V>

§

impl<K, V> Send for Cache<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V> Unpin for Cache<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for Cache<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more