mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-19 08:41:24 +01:00
move eventidshort_cache to service
This commit is contained in:
parent
095ee483ac
commit
2b2b4169df
5 changed files with 49 additions and 39 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use lru_cache::LruCache;
|
||||
use ruma::{events::StateEventType, EventId, RoomId};
|
||||
use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
|
||||
|
||||
use crate::{
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
|
|
@ -38,18 +38,23 @@ pub(crate) use data::Data;
|
|||
pub(crate) struct Service {
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
||||
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub(crate) fn new(
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache_size: usize,
|
||||
eventidshort_cache_size: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
shorteventid_cache: Mutex::new(LruCache::new(
|
||||
shorteventid_cache_size,
|
||||
)),
|
||||
eventidshort_cache: Mutex::new(LruCache::new(
|
||||
eventidshort_cache_size,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +62,29 @@ impl Service {
|
|||
&self,
|
||||
event_id: &EventId,
|
||||
) -> Result<ShortEventId> {
|
||||
self.db.get_or_create_shorteventid(event_id)
|
||||
let lookup = Lookup::CreateEventIdToShort;
|
||||
|
||||
if let Some(short) =
|
||||
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
let (short, created) = self.db.get_or_create_shorteventid(event_id)?;
|
||||
|
||||
if created {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
}
|
||||
|
||||
self.eventidshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(event_id.to_owned(), short);
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
pub(crate) fn get_shortstatekey(
|
||||
|
|
|
|||
|
|
@ -6,10 +6,11 @@ use super::{ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey};
|
|||
use crate::Result;
|
||||
|
||||
pub(crate) trait Data: Send + Sync {
|
||||
/// The returned bool indicates whether it was created
|
||||
fn get_or_create_shorteventid(
|
||||
&self,
|
||||
event_id: &EventId,
|
||||
) -> Result<ShortEventId>;
|
||||
) -> Result<(ShortEventId, bool)>;
|
||||
|
||||
fn get_shortstatekey(
|
||||
&self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue