Local users are not supposed to be fetched by URI
This commit is contained in:
parent
553dbb9b7b
commit
aeb94687b5
|
@ -1,17 +1,20 @@
|
||||||
use crate::web::ApiError;
|
|
||||||
use cached::{Cached, TimedCache};
|
|
||||||
use magnetar_common::config::MagnetarConfig;
|
|
||||||
use magnetar_model::{
|
|
||||||
ck, CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub,
|
|
||||||
InternalStreamMessage, SubMessage,
|
|
||||||
};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use cached::{Cached, TimedCache};
|
||||||
use strum::EnumVariantNames;
|
use strum::EnumVariantNames;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
use magnetar_common::config::MagnetarConfig;
|
||||||
|
use magnetar_model::{
|
||||||
|
CalckeyCache, CalckeyCacheError, CalckeyDbError, CalckeyModel, CalckeySub, ck,
|
||||||
|
InternalStreamMessage, SubMessage,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::web::ApiError;
|
||||||
|
|
||||||
#[derive(Debug, Error, EnumVariantNames)]
|
#[derive(Debug, Error, EnumVariantNames)]
|
||||||
pub enum UserCacheError {
|
pub enum UserCacheError {
|
||||||
#[error("Database error: {0}")]
|
#[error("Database error: {0}")]
|
||||||
|
@ -37,7 +40,6 @@ struct LocalUserCache {
|
||||||
lifetime: TimedCache<String, ()>,
|
lifetime: TimedCache<String, ()>,
|
||||||
id_to_user: HashMap<String, Arc<ck::user::Model>>,
|
id_to_user: HashMap<String, Arc<ck::user::Model>>,
|
||||||
token_to_user: HashMap<String, Arc<ck::user::Model>>,
|
token_to_user: HashMap<String, Arc<ck::user::Model>>,
|
||||||
uri_to_user: HashMap<String, Arc<ck::user::Model>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LocalUserCache {
|
impl LocalUserCache {
|
||||||
|
@ -50,9 +52,6 @@ impl LocalUserCache {
|
||||||
if let Some(token) = user.token.clone() {
|
if let Some(token) = user.token.clone() {
|
||||||
self.token_to_user.remove(&token);
|
self.token_to_user.remove(&token);
|
||||||
}
|
}
|
||||||
if let Some(uri) = user.uri.clone() {
|
|
||||||
self.uri_to_user.remove(&uri);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,10 +65,6 @@ impl LocalUserCache {
|
||||||
if let Some(token) = user.token.clone() {
|
if let Some(token) = user.token.clone() {
|
||||||
self.token_to_user.insert(token, user.clone());
|
self.token_to_user.insert(token, user.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(uri) = user.uri.clone() {
|
|
||||||
self.uri_to_user.insert(uri, user);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Low-priority refresh. Only refreshes the cache if the user is not there.
|
/// Low-priority refresh. Only refreshes the cache if the user is not there.
|
||||||
|
@ -105,19 +100,6 @@ impl LocalUserCache {
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_by_uri(&mut self, uri: &str) -> Option<Arc<ck::user::Model>> {
|
|
||||||
if let Some(user) = self.uri_to_user.get(uri).cloned() {
|
|
||||||
if self.lifetime.cache_get(&user.id).is_none() {
|
|
||||||
self.purge(&user);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Some(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LocalUserCacheService {
|
pub struct LocalUserCacheService {
|
||||||
|
@ -137,7 +119,6 @@ impl LocalUserCacheService {
|
||||||
lifetime: TimedCache::with_lifespan(60 * 5),
|
lifetime: TimedCache::with_lifespan(60 * 5),
|
||||||
id_to_user: HashMap::new(),
|
id_to_user: HashMap::new(),
|
||||||
token_to_user: HashMap::new(),
|
token_to_user: HashMap::new(),
|
||||||
uri_to_user: HashMap::new(),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let cache_clone = cache.clone();
|
let cache_clone = cache.clone();
|
||||||
|
@ -213,21 +194,6 @@ impl LocalUserCacheService {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_uri(
|
|
||||||
&self,
|
|
||||||
uri: &str,
|
|
||||||
) -> Result<Option<Arc<ck::user::Model>>, UserCacheError> {
|
|
||||||
let result = self.cache.lock().await.get_by_uri(uri);
|
|
||||||
|
|
||||||
if let Some(user) = result {
|
|
||||||
return Ok(Some(user));
|
|
||||||
}
|
|
||||||
|
|
||||||
let user = self.db.get_user_by_uri(uri).await?;
|
|
||||||
|
|
||||||
self.map_cache_user(user).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_by_id(
|
pub async fn get_by_id(
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
|
|
Loading…
Reference in New Issue