缓存采用Arc,提高性能
This commit is contained in:
parent
cf5c5286c0
commit
70162a37f6
@ -10,6 +10,22 @@ pub enum Role {
|
||||
User,
|
||||
}
|
||||
|
||||
impl Role {
|
||||
pub fn is_admin(&self) -> bool {
|
||||
match self {
|
||||
Role::Admin => true,
|
||||
Role::User => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_user(&self) -> bool {
|
||||
match self {
|
||||
Role::Admin => false,
|
||||
Role::User => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Role {
|
||||
fn default() -> Self {
|
||||
Role::User
|
||||
|
2
library/src/cache/account_cache.rs
vendored
2
library/src/cache/account_cache.rs
vendored
@ -15,7 +15,7 @@ pub struct CacheAccount {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LOGIN_CACHE: Cache<String, CacheAccount> = {
|
||||
pub static ref LOGIN_CACHE: Cache<String, Arc<CacheAccount>> = {
|
||||
CacheBuilder::new(20480)
|
||||
.name("login_cache")
|
||||
.eviction_policy(EvictionPolicy::lru())
|
||||
|
@ -52,7 +52,7 @@ pub async fn authenticate_ctx(mut req: Request, next: Next) -> Response {
|
||||
req.extensions_mut().insert(
|
||||
Context {
|
||||
account: account.account.clone(),
|
||||
token: account.token
|
||||
token: account.token.clone()
|
||||
});
|
||||
next.run(req).await
|
||||
},
|
||||
|
@ -59,10 +59,10 @@ pub async fn authenticate_google(
|
||||
LOGIN_CACHE
|
||||
.insert(
|
||||
account.id.to_owned(),
|
||||
CacheAccount {
|
||||
Arc::new(CacheAccount {
|
||||
account: Arc::new(account.to_owned()),
|
||||
token: Arc::new(token.to_owned()),
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
@ -98,10 +98,10 @@ pub async fn refresh_token(
|
||||
LOGIN_CACHE
|
||||
.insert(
|
||||
account.id.to_owned(),
|
||||
CacheAccount {
|
||||
Arc::new(CacheAccount {
|
||||
account,
|
||||
token: Arc::new(refresh_token.token.to_owned()),
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
|
@ -39,7 +39,7 @@ pub async fn authenticate_with_password(
|
||||
ACCOUNT_DISABLED
|
||||
)));
|
||||
}
|
||||
if account.role != Role::Admin {
|
||||
if !account.role.is_admin() {
|
||||
tracing::error!("账户不是管理员,无权限");
|
||||
return Err(ResErr::perm(message!(
|
||||
context.get_lang_id(),
|
||||
@ -53,10 +53,10 @@ pub async fn authenticate_with_password(
|
||||
LOGIN_CACHE
|
||||
.insert(
|
||||
account.id.to_owned(),
|
||||
CacheAccount {
|
||||
Arc::new(CacheAccount {
|
||||
account: Arc::new(account.to_owned()),
|
||||
token: Arc::new(token.to_owned()),
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user