move remaining runtime caches into their respective service
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
e1d1dac95e
commit
473b29d524
12 changed files with 108 additions and 85 deletions
|
@ -1,24 +1,31 @@
|
|||
use std::{mem::size_of, sync::Arc};
|
||||
use std::{
|
||||
mem::size_of,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use conduit::{utils, Result};
|
||||
use conduit::{utils, Result, Server};
|
||||
use database::{Database, Map};
|
||||
use lru_cache::LruCache;
|
||||
|
||||
pub(super) struct Data {
|
||||
shorteventid_authchain: Arc<Map>,
|
||||
db: Arc<Database>,
|
||||
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<[u64]>>>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub(super) fn new(db: &Arc<Database>) -> Self {
|
||||
pub(super) fn new(server: &Arc<Server>, db: &Arc<Database>) -> Self {
|
||||
let config = &server.config;
|
||||
let cache_size = f64::from(config.auth_chain_cache_capacity);
|
||||
let cache_size = (cache_size * config.conduit_cache_capacity_modifier) as usize;
|
||||
Self {
|
||||
shorteventid_authchain: db["shorteventid_authchain"].clone(),
|
||||
db: db.clone(),
|
||||
auth_chain_cache: Mutex::new(LruCache::new(cache_size)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Option<Arc<[u64]>>> {
|
||||
// Check RAM cache
|
||||
if let Some(result) = self.db.auth_chain_cache.lock().unwrap().get_mut(key) {
|
||||
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key) {
|
||||
return Ok(Some(Arc::clone(result)));
|
||||
}
|
||||
|
||||
|
@ -37,10 +44,9 @@ impl Data {
|
|||
|
||||
if let Some(chain) = chain {
|
||||
// Cache in RAM
|
||||
self.db
|
||||
.auth_chain_cache
|
||||
self.auth_chain_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.expect("locked")
|
||||
.insert(vec![key[0]], Arc::clone(&chain));
|
||||
|
||||
return Ok(Some(chain));
|
||||
|
@ -63,10 +69,9 @@ impl Data {
|
|||
}
|
||||
|
||||
// Cache in RAM
|
||||
self.db
|
||||
.auth_chain_cache
|
||||
self.auth_chain_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.expect("locked")
|
||||
.insert(key, auth_chain);
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -17,9 +17,9 @@ pub struct Service {
|
|||
}
|
||||
|
||||
impl Service {
|
||||
pub fn build(_server: &Arc<Server>, db: &Arc<Database>) -> Result<Self> {
|
||||
pub fn build(server: &Arc<Server>, db: &Arc<Database>) -> Result<Self> {
|
||||
Ok(Self {
|
||||
db: Data::new(db),
|
||||
db: Data::new(server, db),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -181,4 +181,11 @@ impl Service {
|
|||
self.db
|
||||
.cache_auth_chain(key, auth_chain.iter().copied().collect::<Arc<[u64]>>())
|
||||
}
|
||||
|
||||
pub fn get_cache_usage(&self) -> (usize, usize) {
|
||||
let cache = self.db.auth_chain_cache.lock().expect("locked");
|
||||
(cache.len(), cache.capacity())
|
||||
}
|
||||
|
||||
pub fn clear_cache(&self) { self.db.auth_chain_cache.lock().expect("locked").clear(); }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue