Skip to main content

RedisCache

Struct RedisCache 

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

Redis 缓存封装,提供键值读写、分布式锁、计数器等常用缓存操作。

Implementations§

Source§

impl RedisCache

Source

pub async fn conn(&self) -> Result<RedisClientConn, Error>

Source

pub fn new(client: &'static RedisClient) -> Self

创建新的 RedisCache 实例,默认 TTL 10 分钟,无前缀。

Source

pub fn with_ttl(self, ttl: Duration) -> Self

设置缓存条目的过期时长,支持链式调用。

Source

pub fn with_prefix(self, prefix: impl Into<String>) -> Self

设置所有缓存键的前缀,支持链式调用。

Source

pub async fn ping(&self) -> Result<(), Error>

向 Redis 发送 PING 以检测连接是否正常。

Source

pub async fn lock( &self, key: &str, ttl: Option<Duration>, ) -> Result<bool, Error>

尝试通过 SET NX 获取分布式锁。 返回 true 表示加锁成功,false 表示锁已被持有。

Source

pub async fn del(&self, key: &str) -> Result<(), Error>

删除指定键。

Source

pub async fn incr( &self, key: &str, delta: i64, ttl: Option<Duration>, ) -> Result<i64, Error>

原子性地将计数器累加 delta,返回累加后的值。 键不存在时先用 SET NX 初始化为 0 再执行 INCRBY。

Source

pub async fn set<T: ToSingleRedisArg + Send + Sync>( &self, key: &str, value: T, ttl: Option<Duration>, ) -> Result<(), Error>

向 Redis 写入值,TTL 为 None 时使用实例默认值。

Source

pub async fn get<T: FromRedisValue>(&self, key: &str) -> Result<T, Error>

从 Redis 读取值,类型由泛型参数指定。

Source

pub async fn set_struct<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

将结构体序列化为 JSON 后存入 Redis。

Source

pub async fn get_struct<T>(&self, key: &str) -> Result<Option<T>, Error>

从 Redis 读取并反序列化为结构体,键不存在时返回 None

Source

pub async fn ttl(&self, key: &str) -> Result<i32, Error>

获取指定键的剩余过期时间(秒)。 返回 -2 表示键不存在,-1 表示键无过期时间。

Source

pub async fn get_del<T: FromRedisValue>(&self, key: &str) -> Result<T, Error>

原子性地读取并删除指定键(需 Redis ≥6.2.0)。

Source

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

检查指定键是否存在。

Source

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

刷新指定键的过期时间而不修改其值。 返回 true 表示刷新成功,false 表示键不存在。

Source

pub async fn set_struct_lz4<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

将结构体序列化为 JSON 并以 LZ4 压缩后存入 Redis。 LZ4 压缩速度快,适合对延迟敏感的场景。

Source

pub async fn get_struct_lz4<T>(&self, key: &str) -> Result<Option<T>, Error>

从 Redis 读取并以 LZ4 解压后反序列化为结构体,键不存在时返回 None

Source

pub async fn set_struct_zstd<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

将结构体序列化为 JSON 并以 Zstd 压缩后存入 Redis。 Zstd 压缩率更高,适合对存储空间敏感的场景。

Source

pub async fn get_struct_zstd<T>(&self, key: &str) -> Result<Option<T>, Error>

从 Redis 读取并以 Zstd 解压后反序列化为结构体,键不存在时返回 None

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> 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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,