mirror of
https://github.com/chatmail/core.git
synced 2026-04-19 06:26:30 +03:00
Introduce config caching (#3131)
* Introduce config caching * Changelog * Update CHANGELOG.md Co-authored-by: bjoern <r10s@b44t.com> * Cache a value after reading it Co-authored-by: bjoern <r10s@b44t.com>
This commit is contained in:
27
src/sql.rs
27
src/sql.rs
@@ -3,7 +3,7 @@
|
||||
use async_std::path::Path;
|
||||
use async_std::sync::RwLock;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::convert::TryFrom;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -47,6 +47,8 @@ pub struct Sql {
|
||||
/// None if the database is not open, true if it is open with passphrase and false if it is
|
||||
/// open without a passphrase.
|
||||
is_encrypted: RwLock<Option<bool>>,
|
||||
|
||||
config_cache: RwLock<HashMap<String, Option<String>>>,
|
||||
}
|
||||
|
||||
impl Sql {
|
||||
@@ -55,6 +57,7 @@ impl Sql {
|
||||
dbfile,
|
||||
pool: Default::default(),
|
||||
is_encrypted: Default::default(),
|
||||
config_cache: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,6 +500,7 @@ impl Sql {
|
||||
/// will already have been logged.
|
||||
pub async fn set_raw_config(&self, key: impl AsRef<str>, value: Option<&str>) -> Result<()> {
|
||||
let key = key.as_ref();
|
||||
|
||||
if let Some(value) = value {
|
||||
let exists = self
|
||||
.exists(
|
||||
@@ -523,11 +527,23 @@ impl Sql {
|
||||
.await?;
|
||||
}
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
lock.insert(key.to_string(), value.map(|s| s.to_string()));
|
||||
drop(lock);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get configuration options from the database.
|
||||
pub async fn get_raw_config(&self, key: impl AsRef<str>) -> Result<Option<String>> {
|
||||
let lock = self.config_cache.read().await;
|
||||
let cached = lock.get(key.as_ref()).cloned();
|
||||
drop(lock);
|
||||
|
||||
if let Some(c) = cached {
|
||||
return Ok(c);
|
||||
}
|
||||
|
||||
let value = self
|
||||
.query_get_value(
|
||||
"SELECT value FROM config WHERE keyname=?;",
|
||||
@@ -536,6 +552,10 @@ impl Sql {
|
||||
.await
|
||||
.context(format!("failed to fetch raw config: {}", key.as_ref()))?;
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
lock.insert(key.as_ref().to_string(), value.clone());
|
||||
drop(lock);
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
@@ -573,6 +593,11 @@ impl Sql {
|
||||
.await
|
||||
.map(|s| s.and_then(|r| r.parse().ok()))
|
||||
}
|
||||
|
||||
#[cfg(feature = "internals")]
|
||||
pub fn config_cache(&self) -> &RwLock<HashMap<String, Option<String>>> {
|
||||
&self.config_cache
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn housekeeping(context: &Context) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user