use std::fmt::Debug;
use std::future::Future;
use std::time::Duration;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::errors::NovaXError;
#[derive(Clone, Debug)]
pub enum CachingDurationStrategy {
EachBlock,
Duration(Duration)
}
#[async_trait]
pub trait CachingStrategy: Clone + Send + Sync + Debug {
async fn get_cache<T: Serialize + DeserializeOwned + Send + Sync>(&self, key: u64) -> Result<Option<T>, NovaXError>;
async fn set_cache<T: Serialize + DeserializeOwned + Send + Sync>(&self, key: u64, value: &T) -> Result<(), NovaXError>;
async fn get_or_set_cache<T, FutureGetter, Error>(&self, key: u64, getter: FutureGetter) -> Result<T, Error>
where
T: Serialize + DeserializeOwned + Send + Sync,
FutureGetter: Future<Output=Result<T, Error>> + Send,
Error: From<NovaXError>;
async fn clear(&self) -> Result<(), NovaXError>;
fn with_duration_strategy(&self, strategy: CachingDurationStrategy) -> Self;
#[allow(missing_docs)]
#[deprecated(note = "This function will be removed soon, please use `with_duration_strategy`.")]
fn with_duration(&self, duration: Duration) -> Self {
self.with_duration_strategy(CachingDurationStrategy::Duration(duration))
}
#[allow(missing_docs)]
#[deprecated(note = "This function will be removed soon, please use `with_duration_strategy`.")]
fn until_next_block(&self) -> Self {
self.with_duration_strategy(CachingDurationStrategy::EachBlock)
}
}