use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
#[derive(Debug)]
pub struct ForeignKeyCache<T> {
value: Mutex<Option<Arc<T>>>,
}
impl<T> Default for ForeignKeyCache<T> {
fn default() -> Self {
Self {
value: Mutex::new(None),
}
}
}
impl<T> Clone for ForeignKeyCache<T> {
fn clone(&self) -> Self {
Self {
value: Mutex::new(self.snapshot()),
}
}
}
impl<T> ForeignKeyCache<T> {
pub fn snapshot(&self) -> Option<Arc<T>> {
self.lock().as_ref().map(Arc::clone)
}
pub fn replace(&self, value: Option<Arc<T>>) {
*self.lock() = value;
}
pub fn get_or_load<E>(
&self,
matches: impl FnOnce(&T) -> bool,
load: impl FnOnce() -> Result<Arc<T>, E>,
) -> Result<Arc<T>, E> {
let mut cached = self.lock();
if let Some(value) = cached.as_ref()
&& matches(value)
{
return Ok(Arc::clone(value));
}
*cached = None;
let value = load()?;
*cached = Some(Arc::clone(&value));
Ok(value)
}
fn lock(&self) -> MutexGuard<'_, Option<Arc<T>>> {
self.value.lock().unwrap_or_else(PoisonError::into_inner)
}
}