Skip non-cacheable scenarios
This commit is contained in:
parent
2e4903e603
commit
94cff7c2c8
|
@ -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;
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue