Skip to main content

tauri_plugin_redb_cache/
config.rs

1//! Plugin configuration
2
3use serde::{Deserialize, Serialize};
4
5/// Cache configuration
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct CacheConfig {
8    /// HTTP cache TTL in milliseconds (default: 15 days)
9    pub http_ttl_ms: u64,
10    /// Image cache TTL in milliseconds (default: 15 days)
11    pub image_ttl_ms: u64,
12    /// Memory cache size (default: 100 entries)
13    pub memory_cache_size: usize,
14    /// Compression threshold in bytes (default: 1024)
15    pub compress_threshold: usize,
16    /// Auto cleanup interval in seconds (default: 3600 = 1 hour)
17    pub cleanup_interval_secs: u64,
18    /// Database file name (default: "cache.redb")
19    pub db_filename: String,
20}
21
22impl Default for CacheConfig {
23    fn default() -> Self {
24        Self {
25            http_ttl_ms: 15 * 24 * 60 * 60 * 1000,      // 15 days
26            image_ttl_ms: 15 * 24 * 60 * 60 * 1000,     // 15 days
27            memory_cache_size: 100,
28            compress_threshold: 1024,                    // 1KB
29            cleanup_interval_secs: 3600,                 // 1 hour
30            db_filename: "cache.redb".to_string(),
31        }
32    }
33}
34
35/// Plugin builder
36#[derive(Default)]
37pub struct Builder {
38    pub(crate) config: CacheConfig,
39}
40
41impl Builder {
42    /// Create a new builder with default configuration
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Set HTTP cache TTL in milliseconds
48    pub fn http_ttl_ms(mut self, ttl: u64) -> Self {
49        self.config.http_ttl_ms = ttl;
50        self
51    }
52
53    /// Set image cache TTL in milliseconds
54    pub fn image_ttl_ms(mut self, ttl: u64) -> Self {
55        self.config.image_ttl_ms = ttl;
56        self
57    }
58
59    /// Set memory cache size
60    pub fn memory_cache_size(mut self, size: usize) -> Self {
61        self.config.memory_cache_size = size;
62        self
63    }
64
65    /// Set compression threshold in bytes
66    pub fn compress_threshold(mut self, threshold: usize) -> Self {
67        self.config.compress_threshold = threshold;
68        self
69    }
70
71    /// Set auto cleanup interval in seconds
72    pub fn cleanup_interval_secs(mut self, interval: u64) -> Self {
73        self.config.cleanup_interval_secs = interval;
74        self
75    }
76
77    /// Set database filename
78    pub fn db_filename(mut self, filename: impl Into<String>) -> Self {
79        self.config.db_filename = filename.into();
80        self
81    }
82}