pub struct Cache { /* private fields */ }Expand description
本地缓存实例
基于 moka::sync::Cache 封装,通过类型擦除支持任意类型的存取。
ⓘ
use x_one::xcache;
// 通过全局便捷 API 操作默认缓存
xcache::set("user:123", "Alice".to_string());
let name: Option<String> = xcache::get("user:123");
// 获取命名缓存实例做更精细操作
if let Some(cache) = xcache::c("session") {
cache.set_with_ttl("token", "abc", Duration::from_secs(300));
}Implementations§
Source§impl Cache
impl Cache
Sourcepub fn get<V: Any + Clone + Send + Sync>(&self, key: &str) -> Option<V>
pub fn get<V: Any + Clone + Send + Sync>(&self, key: &str) -> Option<V>
获取缓存值(泛型,通过 downcast 类型转换)
如果 key 存在但类型不匹配,返回 None。
Sourcepub fn set_with_ttl<V: Any + Send + Sync + 'static>(
&self,
key: &str,
value: V,
ttl: Duration,
)
pub fn set_with_ttl<V: Any + Send + Sync + 'static>( &self, key: &str, value: V, ttl: Duration, )
设置缓存值(指定 TTL)
通过 moka Expiry trait 实现 per-entry TTL, 该条目会在指定 TTL 后过期,而非使用全局默认 TTL。
Sourcepub fn entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
获取缓存条目数
Sourcepub fn default_ttl(&self) -> Duration
pub fn default_ttl(&self) -> Duration
获取默认 TTL
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Cache
impl !RefUnwindSafe for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl !UnwindSafe for Cache
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> 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>
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 more