mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 08:11:24 +01:00
move shorteventid_cache to service
This commit is contained in:
parent
7563360bee
commit
47502d1f36
4 changed files with 45 additions and 30 deletions
|
|
@ -1,8 +1,12 @@
|
|||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use lru_cache::LruCache;
|
||||
use ruma::{events::StateEventType, EventId, RoomId};
|
||||
|
||||
use crate::utils::error::Result;
|
||||
use crate::{
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
utils::error::Result,
|
||||
};
|
||||
|
||||
macro_rules! short_id_type {
|
||||
($name:ident) => {
|
||||
|
|
@ -33,12 +37,19 @@ pub(crate) use data::Data;
|
|||
|
||||
pub(crate) struct Service {
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub(crate) fn new(db: &'static dyn Data) -> Self {
|
||||
pub(crate) fn new(
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache_size: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
shorteventid_cache: Mutex::new(LruCache::new(
|
||||
shorteventid_cache_size,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -69,7 +80,25 @@ impl Service {
|
|||
&self,
|
||||
shorteventid: ShortEventId,
|
||||
) -> Result<Arc<EventId>> {
|
||||
self.db.get_eventid_from_short(shorteventid)
|
||||
let lookup = Lookup::ShortToEventId;
|
||||
|
||||
if let Some(id) =
|
||||
self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid)
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Arc::clone(id));
|
||||
}
|
||||
|
||||
let event_id = self.db.get_eventid_from_short(shorteventid)?;
|
||||
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.shorteventid_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(shorteventid, Arc::clone(&event_id));
|
||||
|
||||
Ok(event_id)
|
||||
}
|
||||
|
||||
pub(crate) fn get_statekey_from_short(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue