58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
|
use std::borrow::Cow;
|
||
|
|
||
|
use async_trait::async_trait;
|
||
|
|
||
|
#[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>;
|
||
|
|
||
|
pub struct Config(pub String);
|
||
|
|
||
|
impl Config {
|
||
|
pub fn config<S: serde::de::DeserializeOwned>(self) -> Result<S, toml::de::Error> {
|
||
|
toml::from_str(&self.0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[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 {
|
||
|
async fn new(config: Config) -> CResult<Self>;
|
||
|
|
||
|
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 {}
|