use std::sync::{Arc, Mutex, MutexGuard};
use cached::{Cached, SizedCache};
use crate::core::ClassHash;
type ContractLRUCache<T> = SizedCache<ClassHash, T>;
type LockedClassCache<'a, T> = MutexGuard<'a, ContractLRUCache<T>>;
#[derive(Clone, Debug)]
pub struct GlobalContractCache<T: Clone>(pub Arc<Mutex<ContractLRUCache<T>>>);
impl<T: Clone> GlobalContractCache<T> {
pub fn lock(&self) -> LockedClassCache<'_, T> {
self.0.lock().expect("Global contract cache is poisoned.")
}
pub fn get(&self, class_hash: &ClassHash) -> Option<T> {
self.lock().cache_get(class_hash).cloned()
}
pub fn set(&self, class_hash: ClassHash, contract_class: T) {
self.lock().cache_set(class_hash, contract_class);
}
pub fn clear(&mut self) {
self.lock().cache_clear();
}
pub fn new(cache_size: usize) -> Self {
Self(Arc::new(Mutex::new(ContractLRUCache::<T>::with_size(cache_size))))
}
}