tauri_plugin_cache/
models.rs

1use serde::{de::DeserializeOwned, Deserialize, Serialize};
2
3// The size threshold in bytes after which compression will be applied
4pub const COMPRESSION_THRESHOLD: usize = 1024; // 1KB
5
6/// Supported compression methods
7#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
8#[serde(rename_all = "lowercase")]
9pub enum CompressionMethod {
10    /// Zlib compression (default, balanced speed/ratio)
11    Zlib,
12    /// LZMA2 compression (better compression ratio, slower)
13    Lzma2,
14}
15
16impl Default for CompressionMethod {
17    fn default() -> Self {
18        Self::Zlib
19    }
20}
21
22/// Options for setting an item in the cache
23#[derive(Debug, Clone, Deserialize, Serialize)]
24#[serde(rename_all = "camelCase")]
25pub struct SetItemOptions {
26    /// Time-to-live in seconds
27    pub ttl: Option<u64>,
28    /// Whether to compress the data before storing
29    pub compress: Option<bool>,
30    /// Compression method to use (overrides default)
31    pub compression_method: Option<CompressionMethod>,
32}
33
34/// A cache item with its value and expiration time
35#[derive(Debug, Clone, Deserialize, Serialize)]
36#[serde(rename_all = "camelCase")]
37pub struct CacheItem<T> {
38    /// The stored value
39    pub value: T,
40    /// Unix timestamp in seconds when this item expires (if applicable)
41    pub expires_at: Option<u64>,
42    /// Whether the data is compressed
43    pub is_compressed: Option<bool>,
44}
45
46/// Request to set an item in the cache
47#[derive(Debug, Serialize)]
48#[serde(rename_all = "camelCase")]
49pub struct SetRequest<T>
50where
51    T: Serialize + DeserializeOwned,
52{
53    /// The key to store the value under
54    pub key: String,
55    /// The value to store
56    pub value: T,
57    /// Options for this cache item
58    pub options: Option<SetItemOptions>,
59}
60
61impl<'de, T> serde::Deserialize<'de> for SetRequest<T>
62where
63    T: Serialize + DeserializeOwned,
64{
65    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
66    where
67        D: serde::Deserializer<'de>,
68    {
69        #[derive(Deserialize)]
70        struct Helper {
71            key: String,
72            value: serde_json::Value,
73            options: Option<SetItemOptions>,
74        }
75
76        let helper = Helper::deserialize(deserializer)?;
77
78        let value = serde_json::from_value(helper.value).map_err(serde::de::Error::custom)?;
79
80        Ok(SetRequest {
81            key: helper.key,
82            value,
83            options: helper.options,
84        })
85    }
86}
87
88/// Request to get an item from the cache
89#[derive(Debug, Deserialize, Serialize)]
90#[serde(rename_all = "camelCase")]
91pub struct GetRequest {
92    /// The key to retrieve
93    pub key: String,
94}
95
96/// Request to remove an item from the cache
97#[derive(Debug, Deserialize, Serialize)]
98#[serde(rename_all = "camelCase")]
99pub struct RemoveRequest {
100    /// The key to remove
101    pub key: String,
102}
103
104/// Request to check if an item exists in the cache
105#[derive(Debug, Deserialize, Serialize)]
106#[serde(rename_all = "camelCase")]
107pub struct HasRequest {
108    /// The key to check
109    pub key: String,
110}
111
112/// Enhanced statistics about the cache
113#[derive(Debug, Clone, Default, Deserialize, Serialize)]
114#[serde(rename_all = "camelCase")]
115pub struct CacheStats {
116    /// Total number of items in the cache (including expired items that haven't been cleaned up yet)
117    pub total_size: usize,
118    /// Number of active (non-expired) items in the cache
119    pub active_size: usize,
120}
121
122/// Response containing a boolean value
123#[derive(Debug, Clone, Default, Deserialize, Serialize)]
124#[serde(rename_all = "camelCase")]
125pub struct BooleanResponse {
126    /// The boolean value
127    pub value: bool,
128}
129
130/// Empty response for operations that don't return a value
131#[derive(Debug, Clone, Default, Deserialize, Serialize)]
132#[serde(rename_all = "camelCase")]
133pub struct EmptyResponse {}
134
135/// Enhanced configuration for cache compression
136#[derive(Clone, Serialize, Deserialize, Debug)]
137pub struct CompressionConfig {
138    /// Enable or disable compression
139    pub enabled: bool,
140    /// Compression level (0-9, where 0 is no compression and 9 is max compression)
141    pub level: u32,
142    /// Threshold in bytes after which compression is applied
143    pub threshold: usize,
144    /// Compression method to use
145    pub method: CompressionMethod,
146}
147
148impl Default for CompressionConfig {
149    fn default() -> Self {
150        Self {
151            enabled: false,
152            level: 6, // Default compression level
153            threshold: COMPRESSION_THRESHOLD,
154            method: CompressionMethod::Zlib,
155        }
156    }
157}
158
159/// Configuration options for the cache plugin
160#[derive(Debug, Clone, Deserialize, Serialize)]
161#[serde(rename_all = "camelCase")]
162pub struct CacheConfig {
163    /// Custom directory path for storing cache files
164    pub cache_dir: Option<String>,
165    /// Custom file name for the cache file
166    pub cache_file_name: Option<String>,
167    /// Cleanup interval in seconds
168    pub cleanup_interval: Option<u64>,
169    /// Default compression setting for new items
170    pub default_compression: Option<bool>,
171    /// Compression level (0-9, where 0 is no compression and 9 is max compression)
172    pub compression_level: Option<u32>,
173    /// Threshold in bytes after which compression is applied
174    pub compression_threshold: Option<usize>,
175    /// Compression method to use (zlib or lzma2)
176    pub compression_method: Option<CompressionMethod>,
177}
178
179impl Default for CacheConfig {
180    fn default() -> Self {
181        Self {
182            cache_dir: None,
183            cache_file_name: None,
184            cleanup_interval: Some(60),        // Default 60 seconds
185            default_compression: Some(true),   // Default compression enabled
186            compression_level: Some(6),        // Default medium compression level
187            compression_threshold: Some(1024), // Default 1KB threshold
188            compression_method: Some(CompressionMethod::Zlib), // Default to Zlib
189        }
190    }
191}