use crate::core::command::RedisCommand;
pub const DEFAULT_CACHE_CAPACITY: u64 = 10_000;
pub const MIN_CACHE_CAPACITY: u64 = 100;
pub const MAX_CACHE_CAPACITY: u64 = 10_000_000;
#[derive(Debug, Clone, Copy)]
pub struct CacheCapacity(u64);
impl CacheCapacity {
pub fn new(capacity: u64) -> Option<Self> {
if (MIN_CACHE_CAPACITY..=MAX_CACHE_CAPACITY).contains(&capacity) {
Some(Self(capacity))
} else {
None
}
}
pub fn value(&self) -> u64 {
self.0
}
pub fn default_capacity() -> Self {
Self(DEFAULT_CACHE_CAPACITY)
}
}
impl Default for CacheCapacity {
fn default() -> Self {
Self::default_capacity()
}
}
pub const DEFAULT_REDIS_URL: &str = "redis://localhost:6379";
pub const DEFAULT_REDIS_PORT: u16 = 6379;
pub const DEFAULT_REDIS_HOST: &str = "localhost";
pub const DEFAULT_OTLP_ENDPOINT: &str = "http://localhost:4318";
pub const DEFAULT_POOL_SIZE: usize = 10;
pub const MIN_POOL_SIZE: usize = 1;
pub const MAX_POOL_SIZE: usize = 100;
#[derive(Debug, Clone, Copy)]
pub struct PoolSize(usize);
impl PoolSize {
pub fn new(size: usize) -> Option<Self> {
if (MIN_POOL_SIZE..=MAX_POOL_SIZE).contains(&size) {
Some(Self(size))
} else {
None
}
}
pub fn value(&self) -> usize {
self.0
}
pub fn default_size() -> Self {
Self(DEFAULT_POOL_SIZE)
}
}
impl Default for PoolSize {
fn default() -> Self {
Self::default_size()
}
}
pub const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 5;
pub const DEFAULT_COMMAND_TIMEOUT_SECS: u64 = 5;
pub const DEFAULT_SCAN_COUNT: i64 = 100;
pub const MAX_LUA_SCRIPT_SIZE: usize = 10 * 1024;
pub const MAX_LUA_SCRIPT_KEYS: usize = 100;
pub const MAX_JSON_SIZE: usize = 5 * 1024 * 1024;
pub const MAX_JSON_DEPTH: usize = 64;
pub const MAX_KEY_LENGTH: usize = 1024;
pub const MAX_VALUE_SIZE: usize = 100 * 1024 * 1024;
pub const DEFAULT_BATCH_SIZE: usize = 100;
pub const MAX_BATCH_SIZE: usize = 10_000;
pub const DEFAULT_TTL_SECS: u64 = 3600;
pub const MAX_TTL_SECS: u64 = 30 * 24 * 60 * 60;
pub const PASSWORD_MASK_ASTERISKS: usize = 5;
pub const FORBIDDEN_LUA_COMMANDS: &[RedisCommand] = &[
RedisCommand::FlushAll,
RedisCommand::FlushDb,
RedisCommand::Keys,
RedisCommand::Shutdown,
RedisCommand::Debug,
RedisCommand::Config,
RedisCommand::Save,
RedisCommand::BgSave,
RedisCommand::BgRewriteAof,
RedisCommand::SlaveOf,
RedisCommand::ReplicaOf,
RedisCommand::Cluster,
RedisCommand::Admin,
RedisCommand::Monitor,
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_capacity_valid() {
assert!(CacheCapacity::new(100).is_some());
assert!(CacheCapacity::new(10_000).is_some());
assert!(CacheCapacity::new(10_000_000).is_some());
}
#[test]
fn test_cache_capacity_invalid() {
assert!(CacheCapacity::new(0).is_none());
assert!(CacheCapacity::new(99).is_none());
assert!(CacheCapacity::new(10_000_001).is_none());
}
#[test]
fn test_cache_capacity_default() {
let cap = CacheCapacity::default();
assert_eq!(cap.value(), DEFAULT_CACHE_CAPACITY);
}
#[test]
fn test_cache_capacity_value() {
let cap = CacheCapacity::new(5000).unwrap();
assert_eq!(cap.value(), 5000);
}
#[test]
fn test_cache_capacity_debug() {
let cap = CacheCapacity::new(1000).unwrap();
let debug = format!("{:?}", cap);
assert!(debug.contains("CacheCapacity"));
}
#[test]
fn test_pool_size_valid() {
assert!(PoolSize::new(1).is_some());
assert!(PoolSize::new(10).is_some());
assert!(PoolSize::new(100).is_some());
}
#[test]
fn test_pool_size_invalid() {
assert!(PoolSize::new(0).is_none());
assert!(PoolSize::new(101).is_none());
}
#[test]
fn test_pool_size_default() {
let size = PoolSize::default();
assert_eq!(size.value(), DEFAULT_POOL_SIZE);
}
#[test]
fn test_pool_size_value() {
let size = PoolSize::new(50).unwrap();
assert_eq!(size.value(), 50);
}
#[test]
fn test_pool_size_debug() {
let size = PoolSize::new(10).unwrap();
let debug = format!("{:?}", size);
assert!(debug.contains("PoolSize"));
}
#[test]
fn test_constants_values() {
assert_eq!(DEFAULT_CONNECT_TIMEOUT_SECS, 5);
assert_eq!(DEFAULT_COMMAND_TIMEOUT_SECS, 5);
assert_eq!(DEFAULT_SCAN_COUNT, 100);
assert_eq!(MAX_LUA_SCRIPT_SIZE, 10 * 1024);
assert_eq!(MAX_LUA_SCRIPT_KEYS, 100);
assert_eq!(MAX_JSON_SIZE, 5 * 1024 * 1024);
assert_eq!(MAX_JSON_DEPTH, 64);
assert_eq!(MAX_KEY_LENGTH, 1024);
assert_eq!(MAX_VALUE_SIZE, 100 * 1024 * 1024);
assert_eq!(DEFAULT_BATCH_SIZE, 100);
assert_eq!(MAX_BATCH_SIZE, 10_000);
assert_eq!(DEFAULT_TTL_SECS, 3600);
assert_eq!(MAX_TTL_SECS, 30 * 24 * 60 * 60);
assert_eq!(PASSWORD_MASK_ASTERISKS, 5);
}
#[test]
fn test_redis_url_constants() {
assert_eq!(DEFAULT_REDIS_URL, "redis://localhost:6379");
assert_eq!(DEFAULT_REDIS_PORT, 6379);
assert_eq!(DEFAULT_REDIS_HOST, "localhost");
assert_eq!(DEFAULT_OTLP_ENDPOINT, "http://localhost:4318");
}
#[test]
fn test_forbidden_lua_commands_not_empty() {
assert!(!FORBIDDEN_LUA_COMMANDS.is_empty());
assert!(FORBIDDEN_LUA_COMMANDS.contains(&RedisCommand::FlushAll));
assert!(FORBIDDEN_LUA_COMMANDS.contains(&RedisCommand::Shutdown));
assert!(FORBIDDEN_LUA_COMMANDS.contains(&RedisCommand::Keys));
}
}