Skip to main content

CacheService

Struct CacheService 

Source
pub struct CacheService { /* private fields */ }
Expand description

缓存服务 (提供泛型封装)

Implementations§

Source§

impl CacheService

Source

pub fn new(backend: Box<dyn CacheBackend>) -> Self

从后端创建缓存服务

Source

pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> CacheResult<Option<T>>

获取缓存值

Source

pub async fn set<T: Serialize + ?Sized>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> CacheResult<()>

设置缓存值

Source

pub async fn delete(&self, key: &str) -> CacheResult<bool>

删除缓存键

Source

pub async fn exists(&self, key: &str) -> CacheResult<bool>

检查键是否存在

Source

pub async fn expire(&self, key: &str, ttl: Duration) -> CacheResult<bool>

设置键的过期时间

Source

pub async fn ttl(&self, key: &str) -> CacheResult<Option<Duration>>

获取键的剩余过期时间

Source

pub async fn mget<T: DeserializeOwned>( &self, keys: &[&str], ) -> CacheResult<Vec<Option<T>>>

批量获取缓存值

Source

pub async fn mset<T: Serialize + ?Sized>( &self, items: &[(&str, &T)], ttl: Option<Duration>, ) -> CacheResult<()>

批量设置缓存值

Source

pub async fn mdelete(&self, keys: &[&str]) -> CacheResult<u64>

批量删除缓存键

Source

pub async fn incr(&self, key: &str, delta: i64) -> CacheResult<i64>

自增操作

Source

pub async fn decr(&self, key: &str, delta: i64) -> CacheResult<i64>

自减操作

Source

pub async fn clear(&self) -> CacheResult<()>

清空缓存

Source

pub fn config(&self) -> &CacheConfig

获取配置

Source

pub fn build_key(&self, key: &str) -> String

构建带前缀的完整键

Auto Trait Implementations§

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, 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.