Skip to main content

RedisCache

Struct RedisCache 

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

Redis 缓存客户端

克隆廉价:内部通过 Arc 与多路复用连接管理器共享同一连接。

Implementations§

Source§

impl RedisCache

Source

pub async fn connect(cfg: &RedisConfig) -> Result<Self>

连接 Redis 并校验连通性。

§参数
  • cfg: Redis 配置(URL / 前缀 / 默认 TTL)
§返回
  • Ok(RedisCache): 连接成功且 PING 通过
  • Err: 无法连接或 PING 失败(调用方应据此终止启动)
§副作用

建立到 Redis 的多路复用连接。

Source

pub fn sender_id(&self) -> &str

本实例唯一发送者 ID

Source

pub fn default_ttl_secs(&self) -> u64

JSON 缓存默认 TTL 秒数

Source

pub fn full_key(&self, logical: &str) -> String

把逻辑键加上统一前缀,得到 Redis 实际键

Source

pub fn json_cache_key(key: &str) -> String

生成 JSON 响应缓存逻辑键。

Source

pub fn value_cache_key(namespace: &str, key: &str) -> String

生成结构化值缓存逻辑键。

Source

pub async fn get_bytes(&self, logical: &str) -> Option<Vec<u8>>

读取字节值。

运行期 Redis 抖动时记录告警并返回 None(读路径降级,不影响请求)。

Source

pub async fn set_bytes_ttl(&self, logical: &str, value: &[u8], ttl_secs: u64)

写入字节值并设置 TTL(秒)。

运行期失败仅记录告警,不向上抛错(写缓存属尽力而为)。

Source

pub async fn del(&self, logical: &str)

删除键。

Source

pub async fn del_pattern(&self, logical_pattern: &str)

按逻辑键模式删除 Redis 键。

使用 SCAN MATCH 分批删除,避免 KEYS 阻塞 Redis。

Source

pub async fn get_json_bytes(&self, key: &str) -> Option<Arc<Vec<u8>>>

读取预序列化 JSON 响应缓存。

Source

pub async fn set_json_bytes_ttl(&self, key: &str, value: &[u8], ttl_secs: u64)

写入预序列化 JSON 响应缓存。

Source

pub async fn del_json(&self, key: &str)

删除单个 JSON 响应缓存。

Source

pub async fn del_json_pattern(&self, pattern: &str)

按业务键模式删除 JSON 响应缓存。

Source

pub async fn get_value<T>(&self, namespace: &str, key: &str) -> Option<T>

读取结构化业务值缓存。

Source

pub async fn set_value_ttl<T>( &self, namespace: &str, key: &str, value: &T, ttl_secs: u64, )
where T: Serialize,

写入结构化业务值缓存。

Source

pub async fn del_value(&self, namespace: &str, key: &str)

删除单个结构化业务值缓存。

Source

pub async fn del_value_pattern(&self, namespace: &str, pattern: &str)

按业务键模式删除结构化业务值缓存。

Source

pub async fn set_str_ttl(&self, logical: &str, value: &str, ttl_secs: u64)

写入字符串值并设置 TTL(秒)。

Source

pub async fn get_str(&self, logical: &str) -> Option<String>

读取字符串值。

Source

pub async fn set_nx_ex(&self, logical: &str, value: &str, ttl_secs: u64) -> bool

条件写入:仅当键不存在时写入并设置 TTL(SET key val NX EX ttl)。

用于协作页面锁的原子抢占。

§返回
  • true:本次成功抢占(键原先不存在)
  • false:键已存在(被他人持有)或 Redis 异常
Source

pub async fn expire(&self, logical: &str, ttl_secs: u64)

刷新键 TTL(秒)。用于续约页面锁。

Source

pub async fn hset_ttl( &self, logical: &str, field: &str, value: &str, ttl_secs: u64, )

写入哈希字段并刷新整哈希 TTL(用于项目在线状态表)。

Source

pub async fn hgetall(&self, logical: &str) -> Vec<(String, String)>

读取哈希全部字段(field, value)。

Source

pub async fn hdel(&self, logical: &str, field: &str)

删除哈希字段。

Source

pub async fn publish(&self, channel: &str, payload: &str)

发布一条消息到指定逻辑频道。

Source

pub async fn subscribe_loop<F>(self, channel: &str, handler: F)
where F: FnMut(String) + Send + 'static,

订阅指定逻辑频道,对每条消息调用 handler

内部使用独立 pub/sub 连接;连接异常时按退避重连,持续运行直到进程退出。

§参数
  • channel: 逻辑频道名(自动加前缀)
  • handler: 收到消息载荷时的回调

Trait Implementations§

Source§

impl Clone for RedisCache

Source§

fn clone(&self) -> RedisCache

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

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

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

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

Source§

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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