move shortstatekey_cache to service

This commit is contained in:
Charles Hall 2024-10-08 13:38:12 -07:00
parent 190b788683
commit d3b62e598d
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 35 additions and 35 deletions

View file

@ -7,11 +7,8 @@ use std::{
sync::{Arc, Mutex, RwLock},
};
use lru_cache::LruCache;
use ruma::{
events::{
push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType,
},
events::{push_rules::PushRulesEvent, GlobalAccountDataEventType},
push::Ruleset,
EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
};
@ -236,8 +233,6 @@ pub(crate) struct KeyValueDatabase {
pub(super) senderkey_pusher: Arc<dyn KvTree>,
// Uncategorized trees
pub(super) shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
pub(super) our_real_users_cache:
RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>,
pub(super) appservice_in_room_cache:
@ -461,14 +456,6 @@ impl KeyValueDatabase {
global: builder.open_tree("global")?,
server_signingkeys: builder.open_tree("server_signingkeys")?,
#[allow(
clippy::as_conversions,
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
shortstatekey_cache: Mutex::new(LruCache::new(
(100_000.0 * config.cache_capacity_modifier) as usize,
)),
our_real_users_cache: RwLock::new(HashMap::new()),
appservice_in_room_cache: RwLock::new(HashMap::new()),
lasttimelinecount_cache: Mutex::new(HashMap::new()),

View file

@ -4,7 +4,6 @@ use ruma::{events::StateEventType, EventId, RoomId};
use crate::{
database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service::{
self,
rooms::short::{
@ -130,15 +129,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
&self,
shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> {
let lookup = Lookup::ShortToStateKey;
if let Some(id) =
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone());
}
let bytes = self
.shortstatekey_statekey
.get(&shortstatekey.get().to_be_bytes())?
@ -168,16 +158,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
)
})?;
let result = (event_type, state_key);
METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache
.lock()
.unwrap()
.insert(shortstatekey, result.clone());
Ok(result)
Ok((event_type, state_key))
}
/// Returns `(shortstatehash, already_existed)`

View file

@ -128,6 +128,14 @@ impl Services {
{
(100_000.0 * config.cache_capacity_modifier) as usize
},
#[allow(
clippy::as_conversions,
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
{
(100_000.0 * config.cache_capacity_modifier) as usize
},
),
state: rooms::state::Service {
db,

View file

@ -41,6 +41,8 @@ pub(crate) struct Service {
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
}
impl Service {
@ -49,6 +51,7 @@ impl Service {
shorteventid_cache_size: usize,
eventidshort_cache_size: usize,
statekeyshort_cache_size: usize,
shortstatekey_cache_size: usize,
) -> Self {
Self {
db,
@ -61,6 +64,9 @@ impl Service {
statekeyshort_cache: Mutex::new(LruCache::new(
statekeyshort_cache_size,
)),
shortstatekey_cache: Mutex::new(LruCache::new(
shortstatekey_cache_size,
)),
}
}
@ -189,7 +195,25 @@ impl Service {
&self,
shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> {
self.db.get_statekey_from_short(shortstatekey)
let lookup = Lookup::ShortToStateKey;
if let Some(id) =
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone());
}
let x = self.db.get_statekey_from_short(shortstatekey)?;
METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache
.lock()
.unwrap()
.insert(shortstatekey, x.clone());
Ok(x)
}
/// Returns `(shortstatehash, already_existed)`