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