pub mod memory;
#[cfg(feature = "redis")]
pub mod redis;
use std::fmt::Debug;
use std::pin::Pin;
use serde_json::Value;
use thiserror::Error;
use crate::config::Timeout;
use crate::error::error_impl::impl_into_cot_error;
const CACHE_STORE_ERROR_PREFIX: &str = "cache store error:";
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CacheStoreError {
#[error("{CACHE_STORE_ERROR_PREFIX} backend error: {0}")]
Backend(String),
#[error("{CACHE_STORE_ERROR_PREFIX} serialization error: {0}")]
Serialize(String),
#[error("{CACHE_STORE_ERROR_PREFIX} deserialization error: {0}")]
Deserialize(String),
}
impl_into_cot_error!(CacheStoreError);
pub type CacheStoreResult<T> = Result<T, CacheStoreError>;
pub trait CacheStore: Send + Sync + 'static {
fn get(&self, key: &str) -> impl Future<Output = CacheStoreResult<Option<Value>>> + Send;
fn insert(
&self,
key: String,
value: Value,
expiry: Timeout,
) -> impl Future<Output = CacheStoreResult<()>> + Send;
fn remove(&self, key: &str) -> impl Future<Output = CacheStoreResult<()>> + Send;
fn clear(&self) -> impl Future<Output = CacheStoreResult<()>> + Send;
fn approx_size(&self) -> impl Future<Output = CacheStoreResult<usize>> + Send;
fn contains_key(&self, key: &str) -> impl Future<Output = CacheStoreResult<bool>> + Send;
}
pub(crate) trait BoxCacheStore: Send + Sync + 'static {
fn get<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<Option<Value>>> + Send + 'a>>;
fn insert<'a>(
&'a self,
key: String,
value: Value,
expiry: Timeout,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>>;
fn remove<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>>;
fn clear<'a>(&'a self) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>>;
fn approx_size<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<usize>> + Send + 'a>>;
fn contains_key<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<bool>> + Send + 'a>>;
}
impl<T: CacheStore> BoxCacheStore for T {
fn get<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<Option<Value>>> + Send + 'a>> {
Box::pin(async move { T::get(self, key).await })
}
fn insert<'a>(
&'a self,
key: String,
value: Value,
expiry: Timeout,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>> {
Box::pin(async move { T::insert(self, key, value, expiry).await })
}
fn remove<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>> {
Box::pin(async move { T::remove(self, key).await })
}
fn clear<'a>(&'a self) -> Pin<Box<dyn Future<Output = CacheStoreResult<()>> + Send + 'a>> {
Box::pin(async move { T::clear(self).await })
}
fn approx_size<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<usize>> + Send + 'a>> {
Box::pin(async move { T::approx_size(self).await })
}
fn contains_key<'a>(
&'a self,
key: &'a str,
) -> Pin<Box<dyn Future<Output = CacheStoreResult<bool>> + Send + 'a>> {
Box::pin(async move { T::contains_key(self, key).await })
}
}