use std::collections::HashMap;
use std::future::Future;
use std::time::Instant;
use crate::error::CacheResult;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StoredValue<V> {
Value(V),
Null,
}
#[derive(Clone, Debug)]
pub struct StoredEntry<V> {
pub value: StoredValue<V>,
pub expire_at: Instant,
}
impl<V> StoredEntry<V> {
pub fn is_expired(&self) -> bool {
Instant::now() >= self.expire_at
}
}
pub trait LocalBackend<V>: Clone + Send + Sync + 'static
where
V: Clone + Send + Sync + 'static,
{
fn get(&self, key: &str) -> impl Future<Output = CacheResult<Option<StoredEntry<V>>>> + Send;
fn peek(&self, key: &str) -> impl Future<Output = CacheResult<Option<StoredEntry<V>>>> + Send;
fn mget(
&self,
keys: &[String],
) -> impl Future<Output = CacheResult<HashMap<String, Option<StoredEntry<V>>>>> + Send;
fn set(&self, key: &str, entry: StoredEntry<V>)
-> impl Future<Output = CacheResult<()>> + Send;
fn mset(
&self,
entries: HashMap<String, StoredEntry<V>>,
) -> impl Future<Output = CacheResult<()>> + Send;
fn del(&self, key: &str) -> impl Future<Output = CacheResult<()>> + Send;
fn mdel(&self, keys: &[String]) -> impl Future<Output = CacheResult<()>> + Send;
}
pub trait InvalidationSubscriber: Send {
fn next_message(&mut self) -> impl Future<Output = Option<CacheResult<String>>> + Send;
}
pub trait RemoteBackend<V>: Clone + Send + Sync + 'static
where
V: Clone + Send + Sync + 'static,
{
type Subscriber: InvalidationSubscriber + Send + 'static;
fn get(&self, key: &str) -> impl Future<Output = CacheResult<Option<StoredEntry<V>>>> + Send;
fn mget(
&self,
keys: &[String],
) -> impl Future<Output = CacheResult<HashMap<String, Option<StoredEntry<V>>>>> + Send;
fn set(&self, key: &str, entry: StoredEntry<V>)
-> impl Future<Output = CacheResult<()>> + Send;
fn mset(
&self,
entries: HashMap<String, StoredEntry<V>>,
) -> impl Future<Output = CacheResult<()>> + Send;
fn del(&self, key: &str) -> impl Future<Output = CacheResult<()>> + Send;
fn mdel(&self, keys: &[String]) -> impl Future<Output = CacheResult<()>> + Send;
fn publish(&self, channel: &str, payload: &str)
-> impl Future<Output = CacheResult<()>> + Send;
fn subscribe(
&self,
channel: &str,
) -> impl Future<Output = CacheResult<Self::Subscriber>> + Send;
}