2026-04-23 14:42:51 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use aether_cache::ExpiringMap;
|
2026-06-22 00:07:26 +08:00
|
|
|
use tokio::sync::{Mutex, MutexGuard};
|
2026-04-23 14:42:51 +08:00
|
|
|
|
|
|
|
|
const MAX_ENTRIES: usize = 512;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct SystemConfigCache {
|
|
|
|
|
entries: ExpiringMap<String, Option<serde_json::Value>>,
|
2026-06-22 00:07:26 +08:00
|
|
|
load_guard: Mutex<()>,
|
2026-04-23 14:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for SystemConfigCache {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
entries: ExpiringMap::new(),
|
2026-06-22 00:07:26 +08:00
|
|
|
load_guard: Mutex::new(()),
|
2026-04-23 14:42:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SystemConfigCache {
|
|
|
|
|
pub(crate) fn get(&self, key: &str, ttl: Duration) -> Option<Option<serde_json::Value>> {
|
|
|
|
|
self.entries.get_fresh(&key.to_string(), ttl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn insert(&self, key: String, value: Option<serde_json::Value>, ttl: Duration) {
|
|
|
|
|
self.entries.insert(key, value, ttl, MAX_ENTRIES);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 00:07:26 +08:00
|
|
|
pub(crate) async fn load_guard(&self) -> MutexGuard<'_, ()> {
|
|
|
|
|
self.load_guard.lock().await
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 14:42:51 +08:00
|
|
|
pub(crate) fn clear(&self) {
|
|
|
|
|
self.entries.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|