Skip non-cacheable scenarios
This commit is contained in:
parent
2e4903e603
commit
94cff7c2c8
|
@ -98,13 +98,16 @@ impl EmojiCacheService {
|
||||||
}
|
}
|
||||||
drop(read);
|
drop(read);
|
||||||
|
|
||||||
let emoji = self
|
let emoji = if to_resolve.is_empty() {
|
||||||
.db
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
self.db
|
||||||
.fetch_many_emojis(&to_resolve, host)
|
.fetch_many_emojis(&to_resolve, host)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(Arc::new)
|
.map(Arc::new)
|
||||||
.collect::<Vec<_>>();
|
.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()
|
||||||
|
} else {
|
||||||
|
self.db
|
||||||
.fetch_many_tagged_emojis(&to_resolve)
|
.fetch_many_tagged_emojis(&to_resolve)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(Arc::new)
|
.map(Arc::new)
|
||||||
.collect::<Vec<_>>();
|
.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;
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -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),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue