69 lines
1.5 KiB
Rust
69 lines
1.5 KiB
Rust
#![feature(box_into_inner)]
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use async_trait::async_trait;
|
|
pub use config::PluginConfig;
|
|
use tracing::warn;
|
|
|
|
#[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>),
|
|
}
|
|
|
|
pub struct CacheObject(pub Option<Vec<u8>>);
|
|
|
|
impl CacheObject {
|
|
pub fn into_inner<T>(self) -> CResult<Option<T>>
|
|
where
|
|
T: serde::de::DeserializeOwned,
|
|
{
|
|
let Some(data) = self.0 else {
|
|
return Ok(None)
|
|
};
|
|
bincode::deserialize(&data)
|
|
.map_err(|e| {
|
|
warn!("Malformed cache entry: {e}");
|
|
Error::InvalidEntry
|
|
})
|
|
.map(Some)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
pub trait CacheAdapter {
|
|
async fn new(config: PluginConfig) -> CResult<Self>
|
|
where
|
|
Self: Sized;
|
|
|
|
async fn read(&self, key: &str) -> CResult<CacheObject>;
|
|
|
|
async fn set(
|
|
&mut self,
|
|
key: &str,
|
|
data: &[u8],
|
|
expires_in: Option<std::time::Duration>,
|
|
) -> CResult<()>;
|
|
|
|
async fn invalidate(&mut self, pattern: InvalidatePattern<'_>) -> CResult<u64>;
|
|
|
|
async fn clear(&mut self) -> CResult<u64>;
|
|
}
|