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
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
pub use models::*;
#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;
mod commands;
mod error;
mod models;
pub use error::{Error, Result};
#[cfg(desktop)]
use desktop::Cache;
#[cfg(mobile)]
use mobile::Cache;
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the cache APIs.
pub trait CacheExt<R: Runtime> {
fn cache(&self) -> &Cache<R>;
}
impl<R: Runtime, T: Manager<R>> crate::CacheExt<R> for T {
fn cache(&self) -> &Cache<R> {
self.state::<Cache<R>>().inner()
}
}
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
// Default config
let config = CacheConfig::default();
init_with_config(config)
}
/// Initializes the plugin with custom configuration.
pub fn init_with_config<R: Runtime>(config: CacheConfig) -> TauriPlugin<R> {
// Clone config for use in the closure
let config_clone = config.clone();
Builder::new("cache")
.invoke_handler(tauri::generate_handler![
commands::set,
commands::get,
commands::has,
commands::remove,
commands::clear,
commands::stats
])
.setup(move |app, api| {
// Provide the config manually to the desktop implementation
#[cfg(desktop)]
let cache = {
// Always start from app's cache directory
let base_cache_dir = app.path().app_cache_dir().map_err(|e| {
crate::Error::Cache(format!("Failed to get app cache directory: {}", e))
})?;
// If custom subdirectory is specified, append it to the app cache directory path
let cache_dir = if let Some(custom_dir) = config_clone.cache_dir.as_deref() {
let custom_path = std::path::PathBuf::from(custom_dir);
if custom_path.is_absolute() {
// Instead of absolute path, take only the last component
let path_components: Vec<_> = custom_path
.components()
.filter(|c| !c.as_os_str().is_empty())
.collect();
if let Some(last_component) = path_components.last() {
base_cache_dir.join(last_component.as_os_str())
} else {
base_cache_dir
}
} else {
// Add as a relative path
base_cache_dir.join(custom_dir)
}
} else {
base_cache_dir
};
// Create the cache directory if it doesn't exist
std::fs::create_dir_all(&cache_dir).map_err(|e| {
crate::Error::Cache(format!("Failed to create cache directory: {}", e))
})?;
// Determine the cache file name
let cache_file_name = config_clone
.cache_file_name
.as_deref()
.unwrap_or("tauri_cache.json");
let cache_file_path = cache_dir.join(cache_file_name);
// Get the default compression settings
let default_compression = config_clone.default_compression.unwrap_or(true);
let compression_level = config_clone.compression_level;
let compression_threshold = config_clone.compression_threshold;
let compression_method = config_clone.compression_method;
// Initialize the cache with cleanup interval
let mut cache = desktop::init_with_config(
app,
api,
cache_file_path,
config_clone.cleanup_interval.unwrap_or(60),
)?;
// Initialize with compression settings
cache.init_with_config(
default_compression,
compression_level,
compression_threshold,
compression_method,
);
cache
};
#[cfg(mobile)]
let cache = {
// Always start from app's cache directory
let base_cache_dir = app.path().app_cache_dir().map_err(|e| {
crate::Error::Cache(format!("Failed to get app cache directory: {}", e))
})?;
// If custom subdirectory is specified, append it to the app cache directory path
let cache_dir = if let Some(custom_dir) = config_clone.cache_dir.as_deref() {
let custom_path = std::path::PathBuf::from(custom_dir);
if custom_path.is_absolute() {
// Instead of absolute path, take only the last component
let path_components: Vec<_> = custom_path
.components()
.filter(|c| !c.as_os_str().is_empty())
.collect();
if let Some(last_component) = path_components.last() {
base_cache_dir.join(last_component.as_os_str())
} else {
base_cache_dir
}
} else {
// Add as a relative path
base_cache_dir.join(custom_dir)
}
} else {
base_cache_dir
};
// Create the cache directory if it doesn't exist
std::fs::create_dir_all(&cache_dir).map_err(|e| {
crate::Error::Cache(format!("Failed to create cache directory: {}", e))
})?;
// Determine the cache file name
let cache_file_name = config_clone
.cache_file_name
.as_deref()
.unwrap_or("tauri_cache.json");
let cache_file_path = cache_dir.join(cache_file_name);
// Get the default compression settings
let default_compression = config_clone.default_compression.unwrap_or(true);
let compression_level = config_clone.compression_level;
let compression_threshold = config_clone.compression_threshold;
let compression_method = config_clone.compression_method;
// Initialize the cache with cleanup interval
let mut cache = mobile::init_with_config(
app,
api,
cache_file_path,
config_clone.cleanup_interval.unwrap_or(60),
)?;
// Initialize with compression settings
cache.init_with_config(
default_compression,
compression_level,
compression_threshold,
compression_method,
);
cache
};
app.manage(cache);
Ok(())
})
.build()
}