From ddb12f02f35f8de57d622db7287f062227f1ffa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Fri, 14 Feb 2025 14:15:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=9A=84=E6=9C=89=E6=95=88=E6=9C=9F=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/src/cache/inner_cache.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/library/src/cache/inner_cache.rs b/library/src/cache/inner_cache.rs index dc56cc8..1ce939e 100644 --- a/library/src/cache/inner_cache.rs +++ b/library/src/cache/inner_cache.rs @@ -30,7 +30,7 @@ impl ServerCache where D: Debug + Send + Sync + Clone + Serialize + DeserializeOwned + 'static, { - fn default(cache_type: &str) -> Self { + fn default(cache_type: &str, expire: i64) -> Self { let cache_type = Arc::new(cache_type.to_string()); let cache_type_raw = cache_type.clone(); let cache_eviction_listener: CacheEvictionListener = @@ -60,7 +60,7 @@ where let cache = CacheBuilder::new(20480) .name("login_cache") .eviction_policy(EvictionPolicy::lru()) - .time_to_live(Duration::from_secs(config!().jwt.expires as u64)) + .time_to_live(Duration::from_secs(expire as u64)) .async_eviction_listener(cache_eviction_listener) .build(); ServerCache { cache, cache_type } @@ -120,17 +120,20 @@ struct RedisCache { pub cache_key: String, // 缓存数据 pub data: String, + // 缓存过期时间,单位秒 + pub expires: Option, } impl RedisCache { /// 向redis缓存Hash中插入数据 pub async fn insert(&self) { + let expire = self.expires.unwrap_or(60 * 30); match REDIS_CACHE .hset_ex( &self.cache_type, &self.cache_key, &self.data, - config!().jwt.expires, + expire ) .await { @@ -151,6 +154,7 @@ impl RedisCache { cache_type: cache_type.clone(), cache_key: key, data: value, + expires: None, }; result.push(cache_data); } @@ -180,6 +184,7 @@ pub async fn init_cache() { } lazy_static! { + /// 登录账号缓存 pub static ref LOGIN_ACCOUNT_CACHE: ServerCache> = - ServerCache::default("CACHE_ACCOUNT"); + ServerCache::default("CACHE_ACCOUNT", config!().jwt.expires); } From 46d02fb0f369117e39aed1bd371515dce6fa7da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Fri, 14 Feb 2025 15:16:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/src/cache/inner_cache.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/src/cache/inner_cache.rs b/library/src/cache/inner_cache.rs index 1ce939e..584e990 100644 --- a/library/src/cache/inner_cache.rs +++ b/library/src/cache/inner_cache.rs @@ -12,12 +12,17 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::cache::redis_cache::REDIS_CACHE; +/// 缓存账号信息 #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CacheAccount { pub account: Arc, pub token: Arc, } +/// 内存缓存数据结构,包含缓存类型和缓存数据 +/// 缓存类型用于区分不同的缓存数据,缓存数据用于存储缓存数据 +/// S: 键值类型 +/// D: 缓存数据类型 pub struct ServerCache { pub cache: Cache, pub cache_type: Arc,