#![allow(dead_code)]
pub(crate) mod entry;
pub(crate) mod trait_impl;
pub(crate) mod oxcache_backend;
pub(crate) use oxcache_backend::OxCacheBackend;
use std::collections::HashMap;
use std::hash::Hash;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CacheStrategy {
#[default]
Lru,
Lfu,
Arc,
TwoQueue,
}
#[derive(Debug, Clone)]
pub struct CacheConfig {
pub(crate) capacity: usize,
pub(crate) ttl_secs: u64,
pub(crate) enable_stats: bool,
pub(crate) strategy: CacheStrategy,
}
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
pub(crate) hits: u64,
pub(crate) misses: u64,
pub(crate) total_requests: u64,
pub(crate) hit_rate: f64,
pub(crate) current_size: usize,
pub(crate) evictions: u64,
}
#[derive(Debug, Clone)]
pub struct CacheEntry<V> {
pub(crate) value: V,
pub(crate) created_at: u64,
pub(crate) last_accessed: u64,
pub(crate) access_count: u64,
pub(crate) size: usize,
}
#[async_trait::async_trait]
pub trait Cache<K, V>: Send + Sync
where
K: Hash + Eq + Send + Sync + std::fmt::Debug + 'static,
V: Clone + Send + Sync + 'static,
{
async fn get(&self, key: &K) -> Option<V>;
async fn put(&self, key: K, value: V, size: usize) -> Result<(), String>;
async fn remove(&self, key: &K) -> bool;
async fn clear(&self);
async fn len(&self) -> usize;
async fn is_empty(&self) -> bool;
async fn stats(&self) -> CacheStats;
async fn warm_up(&self, entries: HashMap<K, (V, usize)>) -> Result<(), String>;
}
#[async_trait::async_trait]
pub trait CacheGetOrInsert<K, V>: Send + Sync
where
K: Hash + Eq + Send + Sync + std::fmt::Debug + Clone + 'static,
V: Clone + Send + Sync + 'static,
{
async fn get_or_insert(
&self,
key: K,
value: V,
size: usize,
) -> Result<V, Box<dyn std::error::Error + Send + Sync>>;
}