Files
Aether/apps/aether-gateway/src/cache/system_config.rs
T

40 lines
977 B
Rust
Raw Normal View History

use std::time::Duration;
use aether_cache::ExpiringMap;
2026-06-22 00:07:26 +08:00
use tokio::sync::{Mutex, MutexGuard};
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<()>,
}
impl Default for SystemConfigCache {
fn default() -> Self {
Self {
entries: ExpiringMap::new(),
2026-06-22 00:07:26 +08:00
load_guard: Mutex::new(()),
}
}
}
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
}
pub(crate) fn clear(&self) {
self.entries.clear();
}
}