crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! Unified oxcache module for all caching operations.
//!
//! This module provides:
//! - [`CacheService`] trait — abstract cache interface (get/set/delete/exists)
//! - [`OxcacheService`] — implementation wrapping `oxcache::Cache<String, String>`
//! - Search result caching
//! - DNS caching
//! - Regex caching
//! - Concurrency control (using tokio::sync::Semaphore)
//!
//! Rate limiting is handled by `limiteron_service` via the domain
//! `RateLimitingService` trait — no hand-written token bucket here.

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;

// CacheService Trait

/// Abstract cache service interface.
///
/// Provides basic key-value cache operations backed by oxcache.
/// All methods are async and return `anyhow::Result`.
///
/// # Implementations
///
/// - [`OxcacheService`] — wraps `oxcache::Cache<String, String>`
///
/// # Usage
///
/// ```rust,ignore
/// use crate::infrastructure::oxcache::CacheService;
///
/// async fn example(cache: &dyn CacheService) -> anyhow::Result<> {
///     cache.set("key", "value", 3600).await?;     // TTL = 3600s
///     let val = cache.get("key").await?;           // `Option<String>`
///     assert!(cache.exists("key").await?);
///     cache.delete("key").await?;
///     Ok()
/// }
/// ```
#[async_trait::async_trait]
pub trait CacheService: Send + Sync {
    /// Get value by key.
    ///
    /// Returns `Ok(None)` if key does not exist (not an error).
    fn get(
        &self,
        key: &str,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<Option<String>>> + Send + '_>,
    >;

    /// Set key-value pair with TTL (in seconds).
    ///
    /// If `ttl_seconds` is 0, the entry uses the service's build-time
    /// default TTL.
    fn set(
        &self,
        key: &str,
        value: &str,
        ttl_seconds: u64,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>;

    /// Delete a key.
    ///
    /// Returns `Ok()` even if the key did not exist.
    fn delete(
        &self,
        key: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>;

    /// Check if a key exists.
    fn exists(
        &self,
        key: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<bool>> + Send + '_>>;
}

// Cache Types

/// Search result cache type
pub type SearchCache = Cache<String, Vec<SearchResult>>;

/// Regex cache type - stores compiled regex as string pattern (Regex doesn't impl Serialize)
pub type RegexCacheType = Cache<String, String>;

/// DNS cache entry
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DnsCacheEntry {
    pub ips: Vec<IpAddr>,
    pub remaining_ttl_secs: u64,
}

/// DNS cache type
pub type DnsCache = Cache<String, DnsCacheEntry>;

// Concurrency Control (using Semaphore)

/// Semaphore-based concurrency controller
#[derive(Clone)]
pub struct ConcurrencyController {
    semaphore: Arc<tokio::sync::Semaphore>,
    max_permits: usize,
}

impl ConcurrencyController {
    /// Create a new concurrency controller
    pub fn new(max_permits: usize) -> Self {
        Self {
            semaphore: Arc::new(tokio::sync::Semaphore::new(max_permits)),
            max_permits,
        }
    }

    /// Acquire a permit, returns None if limited
    pub async fn try_acquire(&self) -> Option<ConcurrencyPermit> {
        match self.semaphore.clone().try_acquire_owned() {
            Ok(permit) => Some(ConcurrencyPermit { _permit: permit }),
            Err(_) => None,
        }
    }

    /// Acquire a permit with wait, returns None if timeout
    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,
        }
    }

    /// Get current available permits
    pub fn available_permits(&self) -> usize {
        self.semaphore.available_permits()
    }

    /// Get max permits
    pub fn max_permits(&self) -> usize {
        self.max_permits
    }
}

/// RAII guard for concurrency permit
pub struct ConcurrencyPermit {
    _permit: tokio::sync::OwnedSemaphorePermit,
}

impl Drop for ConcurrencyPermit {
    fn drop(&mut self) {
        // Permit is automatically released when dropped
    }
}

// Key Generation Utilities

/// Generate cache key from prefix and parameters
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
}

/// Generate search cache 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
}

/// Generate DNS cache key
pub fn generate_dns_key(hostname: &str, port: u16) -> String {
    format!("dns:{}:{}", hostname, port)
}

/// Generate regex cache key (using hash for efficiency)
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)
}

// Cache Initialization

/// Create a new oxcache instance with the given settings
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))
}

/// Create a new DNS cache instance
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))
}

/// Create a new Regex cache instance
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))
}

/// Create oxcache (unified cache — no Redis backend)
pub async fn create_tiered_cache(
    settings: &CacheSettings,
) -> Result<Arc<SearchCache>, oxcache::OxCacheError> {
    create_cache(settings).await
}

// Tests

#[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);

        // Should be able to acquire 2 permits
        // 必须持有 permit 变量,否则 permit 在语句结束时立即 drop 并释放回信号量
        let _p1 = controller.try_acquire().await;
        assert!(_p1.is_some());
        let _p2 = controller.try_acquire().await;
        assert!(_p2.is_some());

        // Third should fail (_p1 和 _p2 仍存活,未释放)
        assert!(controller.try_acquire().await.is_none());
    }

    // ConcurrencyController

    #[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);
        // 立即可用,acquire 应在 timeout 内成功
        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);
        // 先占用唯一 permit(持有不释放)
        let _held = controller.try_acquire().await;
        assert_eq!(controller.available_permits(), 0);

        // acquire 应超时返回 None
        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);
        } // permit 在此 drop

        assert_eq!(controller.available_permits(), 1);
        // drop 后应能再次获取
        let again = controller.try_acquire().await;
        assert!(again.is_some());
    }

    // DnsCacheEntry serde

    #[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);
    }

    // Cache 初始化函数

    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();
        // 验证可用:set 后 get 一致
        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() {
        // create_tiered_cache 等价于 create_cache
        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() {
        // 使用不同容量/TTL 验证 settings 真正传入 builder
        let settings = make_test_cache_settings();
        let cache = create_cache(&settings).await.unwrap();
        // 插入超过 capacity 数量的条目不应 panic(moka 自身做淘汰)
        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());
    }

    // generate_search_key: None 参数路径

    #[test]
    fn test_generate_search_key_without_lang_and_country() {
        // lang=None, country=None 时不应包含 lang/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() {
        // 仅 lang=Some 时包含 lang 段,不包含 country 段
        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() {
        // 仅 country=Some 时包含 country 段,不包含 lang 段
        let key = generate_search_key("rust", 20, None, Some("CN"));
        assert_eq!(key, "search:rust:country=CN:limit=20");
    }
}