mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 08:11:24 +01:00
move statekeyshort_cache to service
This commit is contained in:
parent
2b2b4169df
commit
190b788683
5 changed files with 79 additions and 64 deletions
|
|
@ -39,6 +39,8 @@ pub(crate) struct Service {
|
|||
db: &'static dyn Data,
|
||||
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
||||
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||
statekeyshort_cache:
|
||||
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
|
|
@ -46,6 +48,7 @@ impl Service {
|
|||
db: &'static dyn Data,
|
||||
shorteventid_cache_size: usize,
|
||||
eventidshort_cache_size: usize,
|
||||
statekeyshort_cache_size: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
|
|
@ -55,6 +58,9 @@ impl Service {
|
|||
eventidshort_cache: Mutex::new(LruCache::new(
|
||||
eventidshort_cache_size,
|
||||
)),
|
||||
statekeyshort_cache: Mutex::new(LruCache::new(
|
||||
statekeyshort_cache_size,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -92,7 +98,32 @@ impl Service {
|
|||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<Option<ShortStateKey>> {
|
||||
self.db.get_shortstatekey(event_type, state_key)
|
||||
let lookup = Lookup::StateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(*short));
|
||||
}
|
||||
|
||||
let short = self.db.get_shortstatekey(event_type, state_key)?;
|
||||
|
||||
if let Some(short) = short {
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
}
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
pub(crate) fn get_or_create_shortstatekey(
|
||||
|
|
@ -100,7 +131,33 @@ impl Service {
|
|||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<ShortStateKey> {
|
||||
self.db.get_or_create_shortstatekey(event_type, state_key)
|
||||
let lookup = Lookup::CreateStateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
let (short, created) =
|
||||
self.db.get_or_create_shortstatekey(event_type, state_key)?;
|
||||
|
||||
if created {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
}
|
||||
|
||||
self.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
pub(crate) fn get_eventid_from_short(
|
||||
|
|
|
|||
|
|
@ -18,11 +18,12 @@ pub(crate) trait Data: Send + Sync {
|
|||
state_key: &str,
|
||||
) -> Result<Option<ShortStateKey>>;
|
||||
|
||||
/// The returned bool indicates whether it was created
|
||||
fn get_or_create_shortstatekey(
|
||||
&self,
|
||||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<ShortStateKey>;
|
||||
) -> Result<(ShortStateKey, bool)>;
|
||||
|
||||
fn get_eventid_from_short(
|
||||
&self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue