Merge branch 'main' of http://47.95.198.7:30000/gitea/liyunjia/chuanyue-service
This commit is contained in:
commit
d1b0bb70ac
18
library/src/cache/inner_cache.rs
vendored
18
library/src/cache/inner_cache.rs
vendored
@ -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<Account>,
|
||||
pub token: Arc<String>,
|
||||
}
|
||||
|
||||
/// 内存缓存数据结构,包含缓存类型和缓存数据
|
||||
/// 缓存类型用于区分不同的缓存数据,缓存数据用于存储缓存数据
|
||||
/// S: 键值类型
|
||||
/// D: 缓存数据类型
|
||||
pub struct ServerCache<S, D> {
|
||||
pub cache: Cache<S, D>,
|
||||
pub cache_type: Arc<String>,
|
||||
@ -30,7 +35,7 @@ impl<D> ServerCache<String, D>
|
||||
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<D> =
|
||||
@ -60,7 +65,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 +125,20 @@ struct RedisCache {
|
||||
pub cache_key: String,
|
||||
// 缓存数据
|
||||
pub data: String,
|
||||
// 缓存过期时间,单位秒
|
||||
pub expires: Option<i64>,
|
||||
}
|
||||
|
||||
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 +159,7 @@ impl RedisCache {
|
||||
cache_type: cache_type.clone(),
|
||||
cache_key: key,
|
||||
data: value,
|
||||
expires: None,
|
||||
};
|
||||
result.push(cache_data);
|
||||
}
|
||||
@ -180,6 +189,7 @@ pub async fn init_cache() {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// 登录账号缓存
|
||||
pub static ref LOGIN_ACCOUNT_CACHE: ServerCache<String, Arc<CacheAccount>> =
|
||||
ServerCache::default("CACHE_ACCOUNT");
|
||||
ServerCache::default("CACHE_ACCOUNT", config!().jwt.expires);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user