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

View File

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