use crate::error::Result;
use crate::types::Cursor;
use async_trait::async_trait;
use bytes::Bytes;
use std::time::Duration;
pub const MAX_KEY_BYTES: usize = 512;
pub const MAX_VALUE_BYTES: usize = 1024 * 1024;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SetMode {
#[default]
Always,
IfNotExists,
IfExists,
}
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub struct SetOpts {
pub ttl: Option<Duration>,
pub mode: SetMode,
}
impl SetOpts {
pub fn new() -> Self {
Self::default()
}
pub fn with_ttl(mut self, ttl: Duration) -> Self {
self.ttl = Some(ttl);
self
}
pub fn with_mode(mut self, mode: SetMode) -> Self {
self.mode = mode;
self
}
}
#[async_trait]
pub trait Kv: Send + Sync {
async fn get(&self, key: &str) -> Result<Option<Bytes>>;
async fn mget(&self, keys: &[&str]) -> Result<Vec<Option<Bytes>>>;
async fn set(&self, key: &str, value: Bytes, opts: SetOpts) -> Result<bool>;
async fn delete(&self, key: &str) -> Result<bool>;
async fn exists(&self, key: &str) -> Result<bool>;
async fn incr(&self, key: &str, by: i64) -> Result<i64>;
async fn expire(&self, key: &str, ttl: Duration) -> Result<bool>;
async fn compare_and_swap(&self, key: &str, old: Option<Bytes>, new: Bytes) -> Result<bool>;
async fn scan(
&self,
prefix: &str,
cursor: Option<Cursor>,
limit: u32,
) -> Result<(Vec<String>, Option<Cursor>)>;
}
mod memory;
mod postgres;
pub(crate) use memory::MemKv;
pub(crate) use postgres::PgKv;