pub mod memory;
pub mod redis;
pub mod traits;
pub use traits::*;
pub use memory::*;
pub use redis::*;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const DEFAULT_TTL: u64 = 3600;
#[derive(thiserror::Error, Debug)]
pub enum CacheError {
#[error("Redis connection error: {0}")]
RedisConnection(String),
#[error("Redis operation error: {0}")]
RedisOperation(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Cache key not found: {0}")]
NotFound(String),
#[error("Cache error: {0}")]
Other(String),
}
pub type CacheResult<T> = Result<T, CacheError>;
#[derive(Debug, Clone)]
pub struct CacheConfig {
pub default_ttl: u64,
pub max_memory_entries: Option<usize>,
pub redis_pool_size: Option<u32>,
pub key_prefix: Option<String>,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
default_ttl: DEFAULT_TTL,
max_memory_entries: Some(10000),
redis_pool_size: Some(10),
key_prefix: None,
}
}
}
pub struct CacheKey;
impl CacheKey {
pub fn build(namespace: &str, key: &str) -> String {
format!("{}:{}", namespace, key)
}
pub fn user(user_id: &str) -> String {
Self::build("user", user_id)
}
pub fn session(session_id: &str) -> String {
Self::build("session", session_id)
}
pub fn api_response(path: &str, params: &str) -> String {
Self::build("api", &format!("{}:{}", path, params))
}
}