Skip non-cacheable scenarios

This commit is contained in:
Natty 2024-01-16 13:04:53 +01:00
parent 2e4903e603
commit 94cff7c2c8
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
3 changed files with 34 additions and 15 deletions

View File

@ -98,13 +98,16 @@ impl EmojiCacheService {
} }
drop(read); drop(read);
let emoji = self let emoji = if to_resolve.is_empty() {
.db Vec::new()
.fetch_many_emojis(&to_resolve, host) } else {
.await? self.db
.into_iter() .fetch_many_emojis(&to_resolve, host)
.map(Arc::new) .await?
.collect::<Vec<_>>(); .into_iter()
.map(Arc::new)
.collect::<Vec<_>>()
};
resolved.extend(emoji.iter().cloned()); resolved.extend(emoji.iter().cloned());
let mut write = self.cache.lock().await; let mut write = self.cache.lock().await;
@ -148,13 +151,16 @@ impl EmojiCacheService {
} }
drop(read); drop(read);
let emoji = self let emoji = if to_resolve.is_empty() {
.db Vec::new()
.fetch_many_tagged_emojis(&to_resolve) } else {
.await? self.db
.into_iter() .fetch_many_tagged_emojis(&to_resolve)
.map(Arc::new) .await?
.collect::<Vec<_>>(); .into_iter()
.map(Arc::new)
.collect::<Vec<_>>()
};
resolved.extend(emoji.iter().cloned()); resolved.extend(emoji.iter().cloned());
let mut write = self.cache.lock().await; let mut write = self.cache.lock().await;

View File

@ -1,6 +1,7 @@
use crate::web::ApiError; use crate::web::ApiError;
use lru::LruCache; use lru::LruCache;
use magnetar_calckey_model::{ck, CalckeyDbError, CalckeyModel}; use magnetar_calckey_model::{ck, CalckeyDbError, CalckeyModel};
use magnetar_common::config::MagnetarConfig;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use strum::EnumVariantNames; use strum::EnumVariantNames;
@ -44,10 +45,16 @@ pub struct RemoteInstanceCacheService {
cache: Mutex<LruCache<String, CacheEntry>>, cache: Mutex<LruCache<String, CacheEntry>>,
lifetime_max: Duration, lifetime_max: Duration,
db: CalckeyModel, db: CalckeyModel,
config: &'static MagnetarConfig,
} }
impl RemoteInstanceCacheService { impl RemoteInstanceCacheService {
pub(super) fn new(db: CalckeyModel, cache_size: usize, entry_lifetime: Duration) -> Self { pub(super) fn new(
db: CalckeyModel,
config: &'static MagnetarConfig,
cache_size: usize,
entry_lifetime: Duration,
) -> Self {
const CACHE_SIZE: usize = 256; const CACHE_SIZE: usize = 256;
Self { Self {
@ -58,6 +65,7 @@ impl RemoteInstanceCacheService {
)), )),
lifetime_max: entry_lifetime, lifetime_max: entry_lifetime,
db, db,
config,
} }
} }
@ -65,6 +73,10 @@ impl RemoteInstanceCacheService {
&self, &self,
host: &str, host: &str,
) -> Result<Option<Arc<ck::instance::Model>>, RemoteInstanceCacheError> { ) -> Result<Option<Arc<ck::instance::Model>>, RemoteInstanceCacheError> {
if host == self.config.networking.host {
return Ok(None);
}
let mut read = self.cache.lock().await; let mut read = self.cache.lock().await;
if let Some(item) = read.peek(host) { if let Some(item) = read.peek(host) {
if item.created + self.lifetime_max >= Instant::now() { if item.created + self.lifetime_max >= Instant::now() {

View File

@ -51,6 +51,7 @@ impl MagnetarService {
let emoji_cache = emoji_cache::EmojiCacheService::new(db.clone()); let emoji_cache = emoji_cache::EmojiCacheService::new(db.clone());
let remote_instance_cache = instance_cache::RemoteInstanceCacheService::new( let remote_instance_cache = instance_cache::RemoteInstanceCacheService::new(
db.clone(), db.clone(),
config,
256, 256,
Duration::from_secs(100), Duration::from_secs(100),
); );