use crate::config::settings::CacheSettings;
use crate::domain::models::search_result::SearchResult;
use oxcache::Cache;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
pub mod cache_service;
pub use cache_service::OxcacheService;
#[async_trait::async_trait]
pub trait CacheService: Send + Sync {
fn get(
&self,
key: &str,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<Option<String>>> + Send + '_>,
>;
fn set(
&self,
key: &str,
value: &str,
ttl_seconds: u64,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>;
fn delete(
&self,
key: &str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>;
fn exists(
&self,
key: &str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<bool>> + Send + '_>>;
}
pub type SearchCache = Cache<String, Vec<SearchResult>>;
pub type RegexCacheType = Cache<String, String>;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DnsCacheEntry {
pub ips: Vec<IpAddr>,
pub remaining_ttl_secs: u64,
}
pub type DnsCache = Cache<String, DnsCacheEntry>;
#[derive(Clone)]
pub struct ConcurrencyController {
semaphore: Arc<tokio::sync::Semaphore>,
max_permits: usize,
}
impl ConcurrencyController {
pub fn new(max_permits: usize) -> Self {
Self {
semaphore: Arc::new(tokio::sync::Semaphore::new(max_permits)),
max_permits,
}
}
pub async fn try_acquire(&self) -> Option<ConcurrencyPermit> {
match self.semaphore.clone().try_acquire_owned() {
Ok(permit) => Some(ConcurrencyPermit { _permit: permit }),
Err(_) => None,
}
}
pub async fn acquire(&self, timeout: Duration) -> Option<ConcurrencyPermit> {
match tokio::time::timeout(timeout, self.semaphore.clone().acquire_owned()).await {
Ok(Ok(permit)) => Some(ConcurrencyPermit { _permit: permit }),
_ => None,
}
}
pub fn available_permits(&self) -> usize {
self.semaphore.available_permits()
}
pub fn max_permits(&self) -> usize {
self.max_permits
}
}
pub struct ConcurrencyPermit {
_permit: tokio::sync::OwnedSemaphorePermit,
}
impl Drop for ConcurrencyPermit {
fn drop(&mut self) {
}
}
pub fn generate_key(prefix: &str, params: &[(&str, &str)]) -> String {
if params.is_empty() {
return prefix.to_string();
}
let mut key = prefix.to_string();
for (k, v) in params {
key.push(':');
key.push_str(k);
key.push('=');
key.push_str(v);
}
key
}
pub fn generate_search_key(
query: &str,
limit: u32,
lang: Option<&str>,
country: Option<&str>,
) -> String {
let mut key = format!("search:{}", query);
if let Some(l) = lang {
key.push_str(&format!(":lang={}", l));
}
if let Some(c) = country {
key.push_str(&format!(":country={}", c));
}
key.push_str(&format!(":limit={}", limit));
key
}
pub fn generate_dns_key(hostname: &str, port: u16) -> String {
format!("dns:{}:{}", hostname, port)
}
pub fn generate_regex_key(pattern: &str) -> String {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
pattern.hash(&mut hasher);
let hash = hasher.finish();
format!("regex:{:x}", hash)
}
pub async fn create_cache(
settings: &CacheSettings,
) -> Result<Arc<SearchCache>, oxcache::OxCacheError> {
let cache: SearchCache = Cache::builder()
.capacity(settings.memory.capacity)
.ttl(Duration::from_secs(settings.memory.ttl_seconds))
.build()
.await?;
Ok(Arc::new(cache))
}
pub async fn create_dns_cache(
capacity: u64,
ttl_seconds: u64,
) -> Result<Arc<DnsCache>, oxcache::OxCacheError> {
let cache: DnsCache = Cache::builder()
.capacity(capacity)
.ttl(Duration::from_secs(ttl_seconds))
.build()
.await?;
Ok(Arc::new(cache))
}
pub async fn create_regex_cache(
capacity: u64,
ttl_seconds: u64,
) -> Result<Arc<RegexCacheType>, oxcache::OxCacheError> {
let cache: RegexCacheType = Cache::builder()
.capacity(capacity)
.ttl(Duration::from_secs(ttl_seconds))
.build()
.await?;
Ok(Arc::new(cache))
}
pub async fn create_tiered_cache(
settings: &CacheSettings,
) -> Result<Arc<SearchCache>, oxcache::OxCacheError> {
create_cache(settings).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_key() {
assert_eq!(generate_key("search", &[]), "search");
assert_eq!(
generate_key("search", &[("q", "rust"), ("l", "en")]),
"search:q=rust:l=en"
);
}
#[test]
fn test_generate_search_key() {
let key = generate_search_key("rust", 10, Some("en"), Some("US"));
assert_eq!(key, "search:rust:lang=en:country=US:limit=10");
}
#[test]
fn test_generate_dns_key() {
let key = generate_dns_key("example.com", 80);
assert_eq!(key, "dns:example.com:80");
}
#[test]
fn test_generate_regex_key() {
let key1 = generate_regex_key(r"\d+");
let key2 = generate_regex_key(r"\d+");
assert_eq!(key1, key2);
assert!(key1.starts_with("regex:"));
}
#[tokio::test]
async fn test_concurrency_controller() {
let controller = ConcurrencyController::new(2);
let _p1 = controller.try_acquire().await;
assert!(_p1.is_some());
let _p2 = controller.try_acquire().await;
assert!(_p2.is_some());
assert!(controller.try_acquire().await.is_none());
}
#[tokio::test]
async fn test_concurrency_controller_available_and_max_permits() {
let controller = ConcurrencyController::new(3);
assert_eq!(controller.max_permits(), 3);
assert_eq!(controller.available_permits(), 3);
let p1 = controller.try_acquire().await;
assert!(p1.is_some());
assert_eq!(controller.available_permits(), 2);
assert_eq!(controller.max_permits(), 3);
}
#[tokio::test]
async fn test_concurrency_controller_acquire_success() {
let controller = ConcurrencyController::new(1);
let permit = controller.acquire(Duration::from_millis(100)).await;
assert!(permit.is_some());
assert_eq!(controller.available_permits(), 0);
}
#[tokio::test]
async fn test_concurrency_controller_acquire_timeout() {
let controller = ConcurrencyController::new(1);
let _held = controller.try_acquire().await;
assert_eq!(controller.available_permits(), 0);
let result = controller.acquire(Duration::from_millis(50)).await;
assert!(result.is_none());
}
#[tokio::test]
async fn test_concurrency_permit_drop_releases_semaphore() {
let controller = ConcurrencyController::new(1);
{
let _permit = controller.try_acquire().await;
assert!(_permit.is_some());
assert_eq!(controller.available_permits(), 0);
}
assert_eq!(controller.available_permits(), 1);
let again = controller.try_acquire().await;
assert!(again.is_some());
}
#[test]
fn test_dns_cache_entry_serde_roundtrip() {
let entry = DnsCacheEntry {
ips: vec!["192.168.1.1".parse().unwrap(), "::1".parse().unwrap()],
remaining_ttl_secs: 300,
};
let json = serde_json::to_string(&entry).expect("serialize failed");
let decoded: DnsCacheEntry = serde_json::from_str(&json).expect("deserialize failed");
assert_eq!(decoded.ips, entry.ips);
assert_eq!(decoded.remaining_ttl_secs, 300);
}
#[test]
fn test_dns_cache_entry_serde_empty_ips() {
let entry = DnsCacheEntry {
ips: vec![],
remaining_ttl_secs: 0,
};
let json = serde_json::to_string(&entry).expect("serialize failed");
let decoded: DnsCacheEntry = serde_json::from_str(&json).expect("deserialize failed");
assert!(decoded.ips.is_empty());
assert_eq!(decoded.remaining_ttl_secs, 0);
}
fn make_test_cache_settings() -> CacheSettings {
CacheSettings {
enabled: true,
backend: "memory".to_string(),
redis_url: String::new(),
memory: crate::config::settings::MemoryCacheSettings {
capacity: 100,
ttl_seconds: 60,
},
types: crate::config::settings::CacheTypeSpecificSettings {
search: crate::config::settings::CacheTypeSettings {
ttl_seconds: 60,
max_size: 100,
},
dns: crate::config::settings::CacheTypeSettings {
ttl_seconds: 300,
max_size: 100,
},
regex: crate::config::settings::CacheTypeSettings {
ttl_seconds: 600,
max_size: 50,
},
},
}
}
#[tokio::test]
async fn test_create_cache_ok() {
let settings = make_test_cache_settings();
let cache = create_cache(&settings).await;
assert!(
cache.is_ok(),
"create_cache should succeed: {:?}",
cache.err()
);
let cache = cache.unwrap();
let key = "k1".to_string();
let val: Vec<SearchResult> = vec![];
assert!(cache.set(&key, &val).await.is_ok());
let got = cache.get(&key).await;
assert!(got.is_ok(), "get should succeed");
assert!(got.unwrap().is_some(), "value should be present");
}
#[tokio::test]
async fn test_create_dns_cache_ok() {
let cache = create_dns_cache(50, 120).await;
assert!(cache.is_ok(), "create_dns_cache should succeed");
let cache = cache.unwrap();
let key = "dns:example.com:80".to_string();
let entry = DnsCacheEntry {
ips: vec!["1.2.3.4".parse().unwrap()],
remaining_ttl_secs: 120,
};
assert!(cache.set(&key, &entry).await.is_ok());
let got = cache.get(&key).await;
assert!(got.is_ok());
assert!(got.unwrap().is_some());
}
#[tokio::test]
async fn test_create_regex_cache_ok() {
let cache = create_regex_cache(20, 600).await;
assert!(cache.is_ok(), "create_regex_cache should succeed");
let cache = cache.unwrap();
let key = "regex:abc".to_string();
assert!(cache.set(&key, &"pattern".to_string()).await.is_ok());
let got = cache.get(&key).await;
assert!(got.is_ok());
assert!(got.unwrap().is_some());
}
#[tokio::test]
async fn test_create_tiered_cache_ok() {
let settings = make_test_cache_settings();
let cache = create_tiered_cache(&settings).await;
assert!(cache.is_ok(), "create_tiered_cache should succeed");
let _cache = cache.unwrap();
}
#[tokio::test]
async fn test_create_cache_uses_settings_ttl_and_capacity() {
let settings = make_test_cache_settings();
let cache = create_cache(&settings).await.unwrap();
for i in 0..150 {
let key = format!("k{}", i);
let val: Vec<SearchResult> = vec![];
let _ = cache.set(&key, &val).await;
}
let last_key = "k149".to_string();
let got = cache.get(&last_key).await;
assert!(got.is_ok());
}
#[test]
fn test_generate_search_key_without_lang_and_country() {
let key = generate_search_key("rust", 10, None, None);
assert_eq!(key, "search:rust:limit=10");
}
#[test]
fn test_generate_search_key_with_only_lang() {
let key = generate_search_key("rust", 5, Some("zh"), None);
assert_eq!(key, "search:rust:lang=zh:limit=5");
}
#[test]
fn test_generate_search_key_with_only_country() {
let key = generate_search_key("rust", 20, None, Some("CN"));
assert_eq!(key, "search:rust:country=CN:limit=20");
}
}