pub struct RedisCache { /* private fields */ }Expand description
Redis 缓存客户端
克隆廉价:内部通过 Arc 与多路复用连接管理器共享同一连接。
Implementations§
Source§impl RedisCache
impl RedisCache
Sourcepub async fn connect(cfg: &RedisConfig) -> Result<Self>
pub async fn connect(cfg: &RedisConfig) -> Result<Self>
Sourcepub fn default_ttl_secs(&self) -> u64
pub fn default_ttl_secs(&self) -> u64
JSON 缓存默认 TTL 秒数
Sourcepub fn json_cache_key(key: &str) -> String
pub fn json_cache_key(key: &str) -> String
生成 JSON 响应缓存逻辑键。
Sourcepub fn value_cache_key(namespace: &str, key: &str) -> String
pub fn value_cache_key(namespace: &str, key: &str) -> String
生成结构化值缓存逻辑键。
Sourcepub async fn get_bytes(&self, logical: &str) -> Option<Vec<u8>>
pub async fn get_bytes(&self, logical: &str) -> Option<Vec<u8>>
读取字节值。
运行期 Redis 抖动时记录告警并返回 None(读路径降级,不影响请求)。
Sourcepub async fn set_bytes_ttl(&self, logical: &str, value: &[u8], ttl_secs: u64)
pub async fn set_bytes_ttl(&self, logical: &str, value: &[u8], ttl_secs: u64)
写入字节值并设置 TTL(秒)。
运行期失败仅记录告警,不向上抛错(写缓存属尽力而为)。
Sourcepub async fn del_pattern(&self, logical_pattern: &str)
pub async fn del_pattern(&self, logical_pattern: &str)
按逻辑键模式删除 Redis 键。
使用 SCAN MATCH 分批删除,避免 KEYS 阻塞 Redis。
Sourcepub async fn set_json_bytes_ttl(&self, key: &str, value: &[u8], ttl_secs: u64)
pub async fn set_json_bytes_ttl(&self, key: &str, value: &[u8], ttl_secs: u64)
写入预序列化 JSON 响应缓存。
Sourcepub async fn del_json_pattern(&self, pattern: &str)
pub async fn del_json_pattern(&self, pattern: &str)
按业务键模式删除 JSON 响应缓存。
Sourcepub async fn get_value<T>(&self, namespace: &str, key: &str) -> Option<T>where
T: DeserializeOwned,
pub async fn get_value<T>(&self, namespace: &str, key: &str) -> Option<T>where
T: DeserializeOwned,
读取结构化业务值缓存。
Sourcepub async fn set_value_ttl<T>(
&self,
namespace: &str,
key: &str,
value: &T,
ttl_secs: u64,
)where
T: Serialize,
pub async fn set_value_ttl<T>(
&self,
namespace: &str,
key: &str,
value: &T,
ttl_secs: u64,
)where
T: Serialize,
写入结构化业务值缓存。
Sourcepub async fn del_value_pattern(&self, namespace: &str, pattern: &str)
pub async fn del_value_pattern(&self, namespace: &str, pattern: &str)
按业务键模式删除结构化业务值缓存。
Sourcepub async fn set_str_ttl(&self, logical: &str, value: &str, ttl_secs: u64)
pub async fn set_str_ttl(&self, logical: &str, value: &str, ttl_secs: u64)
写入字符串值并设置 TTL(秒)。
Sourcepub async fn set_nx_ex(&self, logical: &str, value: &str, ttl_secs: u64) -> bool
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 异常
Trait Implementations§
Source§impl Clone for RedisCache
impl Clone for RedisCache
Source§fn clone(&self) -> RedisCache
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RedisCache
impl !RefUnwindSafe for RedisCache
impl Send for RedisCache
impl Sync for RedisCache
impl Unpin for RedisCache
impl UnsafeUnpin for RedisCache
impl !UnwindSafe for RedisCache
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request