use crate::store::CacheStore;
use doido_core::Result;
use std::sync::{Arc, OnceLock};
static CACHE: OnceLock<Arc<dyn CacheStore>> = OnceLock::new();
pub async fn init() -> Result<Arc<dyn CacheStore>> {
if let Some(existing) = CACHE.get() {
return Ok(existing.clone());
}
let store = crate::config::load().build().await?;
let _ = CACHE.set(store);
Ok(CACHE.get().expect("cache was just set").clone())
}
pub fn set_store(store: Arc<dyn CacheStore>) -> std::result::Result<(), Arc<dyn CacheStore>> {
CACHE.set(store)
}
pub fn store() -> Arc<dyn CacheStore> {
CACHE
.get()
.expect("cache not initialised; call doido_cache::global::init() at boot")
.clone()
}
pub fn try_store() -> Option<Arc<dyn CacheStore>> {
CACHE.get().cloned()
}