conduwuit/src/service/rooms/auth_chain/mod.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

mod data;
use std::{sync::Arc, collections::HashSet};
pub use data::Data;
2021-08-12 23:04:00 +02:00
2022-09-07 13:25:51 +02:00
use crate::Result;
2021-08-14 19:07:50 +02:00
pub struct Service<D: Data> {
db: D,
}
2022-06-20 12:08:58 +02:00
2022-09-07 13:25:51 +02:00
impl<D: Data> Service<D> {
2022-06-19 22:56:14 +02:00
#[tracing::instrument(skip(self))]
pub fn get_cached_eventid_authchain<'a>(
2022-06-19 22:56:14 +02:00
&'a self,
key: &[u64],
) -> Result<Option<Arc<HashSet<u64>>>> {
// Check RAM cache
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key.to_be_bytes()) {
2022-06-19 22:56:14 +02:00
return Ok(Some(Arc::clone(result)));
}
// We only save auth chains for single events in the db
2022-10-05 09:34:25 +02:00
if key.len() == 1 {
// Check DB cache
if let Some(chain) = self.db.get_cached_eventid_authchain(key[0])
2022-06-19 22:56:14 +02:00
{
let chain = Arc::new(chain);
2022-06-19 22:56:14 +02:00
// Cache in RAM
self.auth_chain_cache
.lock()
.unwrap()
.insert(vec![key[0]], Arc::clone(&chain));
2021-08-12 23:04:00 +02:00
2022-06-19 22:56:14 +02:00
return Ok(Some(chain));
2021-08-12 23:04:00 +02:00
}
}
2022-06-19 22:56:14 +02:00
Ok(None)
2021-04-11 21:01:27 +02:00
}
2021-02-28 12:41:03 +01:00
#[tracing::instrument(skip(self))]
pub fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()> {
// Only persist single events in db
2022-06-19 22:56:14 +02:00
if key.len() == 1 {
self.db.cache_auth_chain(key[0], auth_chain)?;
2020-09-12 21:30:07 +02:00
}
2021-06-08 18:10:00 +02:00
2022-06-19 22:56:14 +02:00
// Cache in RAM
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain);
2022-06-19 22:56:14 +02:00
Ok(())
}
}