use crate::infrastructure::oxcache::RegexCacheType;
use log::warn;
use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
pub trait RegexCacheTrait: Send + Sync {
fn get_or_insert(&self, pattern: &str) -> Result<Regex, String>;
fn get_or_insert_escaped(&self, literal: &str) -> Result<Regex, String>;
fn get_or_compile(&self, pattern: &str) -> Result<Arc<Regex>, String>;
}
#[derive(Clone)]
pub struct RegexCache {
cache: Arc<RegexCacheType>,
compiled: Arc<RwLock<HashMap<String, Arc<Regex>>>>,
}
pub type RegexCacheComponent = RegexCache;
impl RegexCache {
pub fn new(cache: Arc<RegexCacheType>) -> Self {
Self {
cache,
compiled: Arc::new(RwLock::new(HashMap::new())),
}
}
#[inline]
pub fn get_or_insert(&self, pattern: &str) -> Result<Regex, String> {
let key = format!("regex:{}", pattern);
let compiled_read = self.compiled.read().unwrap_or_else(|e| e.into_inner());
if let Some(regex) = compiled_read.get(&key) {
return Ok((**regex).clone());
}
drop(compiled_read);
let regex = Regex::new(pattern).map_err(|e| e.to_string())?;
let mut compiled_write = self.compiled.write().unwrap_or_else(|e| e.into_inner());
compiled_write.insert(key.clone(), Arc::new(regex.clone()));
self.try_persist_to_cache(&key, pattern);
Ok(regex)
}
#[inline]
pub fn get_or_insert_escaped(&self, literal: &str) -> Result<Regex, String> {
let pattern = format!(r"\b{}\b", regex::escape(literal));
self.get_or_insert(&pattern)
}
#[inline]
pub fn get_or_compile(&self, pattern: &str) -> Result<Arc<Regex>, String> {
let key = format!("regex:{}", pattern);
let compiled_read = self.compiled.read().unwrap_or_else(|e| e.into_inner());
if let Some(regex) = compiled_read.get(&key) {
return Ok(regex.clone());
}
drop(compiled_read);
let regex = Regex::new(pattern).map_err(|e| e.to_string())?;
let regex_arc = Arc::new(regex);
let mut compiled_write = self.compiled.write().unwrap_or_else(|e| e.into_inner());
compiled_write.insert(key.clone(), regex_arc.clone());
self.try_persist_to_cache(&key, pattern);
Ok(regex_arc)
}
fn try_persist_to_cache(&self, key: &str, value: &str) {
if let Ok(handle) = tokio::runtime::Handle::try_current() {
let cache = Arc::clone(&self.cache);
let key = key.to_string();
let value = value.to_string();
drop(handle.spawn(async move {
if let Err(e) = cache.set(&key, &value).await {
warn!("Failed to cache regex pattern: {}", e);
}
}));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
async fn make_cache() -> RegexCache {
let cache: RegexCacheType = oxcache::Cache::builder()
.capacity(100)
.ttl(Duration::from_secs(3600))
.build()
.await
.unwrap();
RegexCache::new(Arc::new(cache))
}
#[tokio::test]
async fn test_regex_cache_creation() {
let cache: RegexCacheType = oxcache::Cache::builder()
.capacity(100)
.ttl(Duration::from_secs(3600))
.build()
.await
.unwrap();
let regex_cache = RegexCache::new(Arc::new(cache));
let regex = regex_cache.get_or_insert(r"\d+").unwrap();
assert!(regex.is_match("123"));
}
#[tokio::test]
async fn test_get_or_insert_caches_on_second_call() {
let regex_cache = make_cache().await;
let regex1 = regex_cache.get_or_insert(r"\d+").unwrap();
let regex2 = regex_cache.get_or_insert(r"\d+").unwrap();
assert!(regex1.is_match("123"));
assert!(regex2.is_match("456"));
}
#[tokio::test]
async fn test_get_or_insert_different_patterns() {
let regex_cache = make_cache().await;
let digits = regex_cache.get_or_insert(r"\d+").unwrap();
let letters = regex_cache.get_or_insert(r"[a-z]+").unwrap();
assert!(digits.is_match("123"));
assert!(!digits.is_match("abc"));
assert!(letters.is_match("abc"));
assert!(!letters.is_match("123"));
}
#[tokio::test]
async fn test_get_or_insert_invalid_regex_returns_error() {
let regex_cache = make_cache().await;
let result = regex_cache.get_or_insert(r"[invalid");
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(!err_msg.is_empty());
}
#[tokio::test]
async fn test_get_or_insert_escaped_matches_literal() {
let regex_cache = make_cache().await;
let regex = regex_cache.get_or_insert_escaped("a.b").unwrap();
assert!(regex.is_match("a.b"));
assert!(!regex.is_match("axb"));
}
#[tokio::test]
async fn test_get_or_insert_escaped_special_chars() {
let regex_cache = make_cache().await;
let regex = regex_cache.get_or_insert_escaped("$pecial").unwrap();
assert!(regex.is_match("cost$pecial here"));
}
#[tokio::test]
async fn test_get_or_compile_returns_arc() {
let regex_cache = make_cache().await;
let regex = regex_cache.get_or_compile(r"\w+").unwrap();
assert!(regex.is_match("hello_world"));
}
#[tokio::test]
async fn test_get_or_compile_caches_on_second_call() {
let regex_cache = make_cache().await;
let regex1 = regex_cache.get_or_compile(r"\d+").unwrap();
let regex2 = regex_cache.get_or_compile(r"\d+").unwrap();
assert!(Arc::ptr_eq(®ex1, ®ex2));
}
#[tokio::test]
async fn test_get_or_compile_invalid_regex_returns_error() {
let regex_cache = make_cache().await;
let result = regex_cache.get_or_compile(r"(unclosed");
assert!(result.is_err());
}
#[tokio::test]
async fn test_get_or_compile_different_patterns_return_different_arcs() {
let regex_cache = make_cache().await;
let regex1 = regex_cache.get_or_compile(r"\d+").unwrap();
let regex2 = regex_cache.get_or_compile(r"[a-z]+").unwrap();
assert!(!Arc::ptr_eq(®ex1, ®ex2));
}
#[tokio::test]
async fn test_regex_cache_clone_preserves_state() {
let regex_cache = make_cache().await;
let _ = regex_cache.get_or_insert(r"\d+").unwrap();
let cloned = regex_cache.clone();
let regex = cloned.get_or_insert(r"\d+").unwrap();
assert!(regex.is_match("123"));
}
#[tokio::test]
async fn test_regex_cache_component_type_alias() {
let regex_cache = make_cache().await;
let _: &RegexCacheComponent = ®ex_cache;
}
use async_trait::async_trait;
use oxcache::backend::{BackendKind, CacheConnector, CacheReader, CacheWriter};
use oxcache::OxCacheResult;
use std::collections::HashMap;
struct FailingBackend;
#[async_trait]
impl CacheReader for FailingBackend {
async fn get(&self, _key: &str) -> OxCacheResult<Option<Vec<u8>>> {
Ok(None)
}
async fn exists(&self, _key: &str) -> OxCacheResult<bool> {
Ok(false)
}
async fn ttl(&self, _key: &str) -> OxCacheResult<Option<Duration>> {
Ok(None)
}
async fn len(&self) -> OxCacheResult<u64> {
Ok(0)
}
async fn capacity(&self) -> OxCacheResult<u64> {
Ok(0)
}
async fn stats(&self) -> OxCacheResult<HashMap<String, String>> {
Ok(HashMap::new())
}
}
#[async_trait]
impl CacheWriter for FailingBackend {
async fn set(
&self,
_key: Arc<str>,
_value: Arc<Vec<u8>>,
_ttl: Option<Duration>,
) -> OxCacheResult<()> {
Err(oxcache::OxCacheError::Operation(
"FailingBackend always fails".to_string(),
))
}
async fn delete(&self, _key: &str) -> OxCacheResult<()> {
Ok(())
}
async fn clear(&self) -> OxCacheResult<()> {
Ok(())
}
async fn expire(&self, _key: &str, _ttl: Duration) -> OxCacheResult<bool> {
Ok(false)
}
}
#[async_trait]
impl CacheConnector for FailingBackend {
async fn health_check(&self) -> OxCacheResult<()> {
Ok(())
}
async fn shutdown(&self) {}
fn backend_kind(&self) -> BackendKind {
BackendKind::Unknown
}
}
async fn make_failing_cache() -> RegexCache {
let cache: RegexCacheType = oxcache::Cache::builder()
.backend_arc(std::sync::Arc::new(FailingBackend))
.build()
.await
.expect("build with FailingBackend should succeed");
RegexCache::new(std::sync::Arc::new(cache))
}
#[tokio::test]
async fn test_get_or_insert_warns_when_cache_set_fails() {
let regex_cache = make_failing_cache().await;
let regex = regex_cache
.get_or_insert(r"\d+")
.expect("get_or_insert should succeed even when cache.set fails");
assert!(regex.is_match("123"));
}
#[tokio::test]
async fn test_get_or_compile_warns_when_cache_set_fails() {
let regex_cache = make_failing_cache().await;
let regex = regex_cache
.get_or_compile(r"[a-z]+")
.expect("get_or_compile should succeed even when cache.set fails");
assert!(regex.is_match("hello"));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_concurrent_get_or_insert_no_deadlock() {
use std::time::Duration;
let cache = Arc::new(make_cache().await);
let mut handles = Vec::new();
for i in 0..8u32 {
let c = Arc::clone(&cache);
handles.push(tokio::task::spawn_blocking(move || {
let pattern = match i % 4 {
0 => r"\d+",
1 => r"[a-z]+",
2 => r"\w+",
_ => r"[A-Z]+",
};
c.get_or_insert(pattern).map(|_| ())
}));
}
for handle in handles {
let result = tokio::time::timeout(Duration::from_secs(5), handle)
.await
.expect("concurrent get_or_insert must not deadlock (timed out)");
result
.expect("task should not panic")
.expect("regex should compile");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_concurrent_get_or_compile_no_deadlock() {
use std::time::Duration;
let cache = Arc::new(make_cache().await);
let mut handles = Vec::new();
for i in 0..8u32 {
let c = Arc::clone(&cache);
handles.push(tokio::task::spawn_blocking(move || {
let pattern = match i % 4 {
0 => r"\d+",
1 => r"[a-z]+",
2 => r"\w+",
_ => r"[A-Z]+",
};
c.get_or_compile(pattern).map(|_| ())
}));
}
for handle in handles {
let result = tokio::time::timeout(Duration::from_secs(5), handle)
.await
.expect("concurrent get_or_compile must not deadlock (timed out)");
result
.expect("task should not panic")
.expect("regex should compile");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_concurrent_mixed_operations_no_deadlock() {
use std::time::Duration;
let cache = Arc::new(make_cache().await);
let patterns = [r"\d+", r"[a-z]+", r"\w+", r"[A-Z]+", r"\s+"];
let mut handles = Vec::new();
for i in 0..16u32 {
let c = Arc::clone(&cache);
let pattern = patterns[(i % 5) as usize].to_string();
handles.push(tokio::task::spawn_blocking(move || {
if i % 2 == 0 {
c.get_or_insert(&pattern).map(|_| ())
} else {
c.get_or_compile(&pattern).map(|_| ())
}
}));
}
for handle in handles {
let result = tokio::time::timeout(Duration::from_secs(5), handle)
.await
.expect("concurrent mixed operations must not deadlock (timed out)");
result
.expect("task should not panic")
.expect("regex should compile");
}
}
}