bazzar/crates/cache-adapter/src/lib.rs

51 lines
1.1 KiB
Rust
Raw Normal View History

2023-06-01 10:41:58 +02:00
use std::borrow::Cow;
use async_trait::async_trait;
2023-06-01 22:02:47 +02:00
pub use config::PluginConfig;
2023-06-01 10:41:58 +02:00
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Unable to connect")]
Connect,
#[error("Entry is invalid")]
InvalidEntry,
#[error("Can't invalidate keys {0:?}")]
Invalidate(Vec<String>),
#[error("Failed to parse config")]
InvalidConfig,
}
pub type CResult<T> = Result<T, Error>;
#[derive(Debug)]
pub enum InvalidatePattern<'s> {
StartsWith(Cow<'s, str>),
EndsWith(Cow<'s, str>),
Contains(Cow<'s, str>),
Const(Cow<'s, str>),
}
#[async_trait]
pub trait CacheAdapter: Sized {
2023-06-01 22:02:47 +02:00
async fn new(config: PluginConfig) -> CResult<Self>;
2023-06-01 10:41:58 +02:00
async fn read<T>(&mut self, key: &str) -> CResult<Option<T>>
where
T: serde::de::DeserializeOwned;
async fn set<T>(
&mut self,
key: &str,
data: T,
expires_in: Option<std::time::Duration>,
) -> CResult<()>
where
T: serde::Serialize + Send;
async fn invalidate(&mut self, pattern: InvalidatePattern<'_>) -> CResult<u64>;
async fn clear(&mut self) -> CResult<u64>;
}
pub struct CacheStorage {}