tauri_plugin_cache/
lib.rs1use tauri::{
2 plugin::{Builder, TauriPlugin},
3 Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(desktop)]
9mod desktop;
10#[cfg(mobile)]
11mod mobile;
12
13mod commands;
14mod error;
15mod models;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::Cache;
21#[cfg(mobile)]
22use mobile::Cache;
23
24pub trait CacheExt<R: Runtime> {
26 fn cache(&self) -> &Cache<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::CacheExt<R> for T {
30 fn cache(&self) -> &Cache<R> {
31 self.state::<Cache<R>>().inner()
32 }
33}
34
35pub fn init<R: Runtime>() -> TauriPlugin<R> {
37 let config = CacheConfig::default();
39 init_with_config(config)
40}
41
42pub fn init_with_config<R: Runtime>(config: CacheConfig) -> TauriPlugin<R> {
44 let config_clone = config.clone();
46
47 Builder::new("cache")
48 .invoke_handler(tauri::generate_handler![
49 commands::set,
50 commands::get,
51 commands::has,
52 commands::remove,
53 commands::clear,
54 commands::stats
55 ])
56 .setup(move |app, api| {
57 #[cfg(desktop)]
59 let cache = {
60 let base_cache_dir = app.path().app_cache_dir().map_err(|e| {
62 crate::Error::Cache(format!("Failed to get app cache directory: {}", e))
63 })?;
64
65 let cache_dir = if let Some(custom_dir) = config_clone.cache_dir.as_deref() {
67 let custom_path = std::path::PathBuf::from(custom_dir);
68 if custom_path.is_absolute() {
69 let path_components: Vec<_> = custom_path
71 .components()
72 .filter(|c| !c.as_os_str().is_empty())
73 .collect();
74
75 if let Some(last_component) = path_components.last() {
76 base_cache_dir.join(last_component.as_os_str())
77 } else {
78 base_cache_dir
79 }
80 } else {
81 base_cache_dir.join(custom_dir)
83 }
84 } else {
85 base_cache_dir
86 };
87
88 std::fs::create_dir_all(&cache_dir).map_err(|e| {
90 crate::Error::Cache(format!("Failed to create cache directory: {}", e))
91 })?;
92
93 let cache_file_name = config_clone
95 .cache_file_name
96 .as_deref()
97 .unwrap_or("tauri_cache.json");
98 let cache_file_path = cache_dir.join(cache_file_name);
99
100 let default_compression = config_clone.default_compression.unwrap_or(false);
102 let compression_level = config_clone.compression_level;
103 let compression_threshold = config_clone.compression_threshold;
104
105 let mut cache = desktop::init_with_config(
107 app,
108 api,
109 cache_file_path,
110 config_clone.cleanup_interval.unwrap_or(60),
111 )?;
112
113 cache.init_with_config(default_compression, compression_level, compression_threshold);
115 cache
116 };
117
118 #[cfg(mobile)]
119 let cache = {
120 let base_cache_dir = app.path().app_cache_dir().map_err(|e| {
122 crate::Error::Cache(format!("Failed to get app cache directory: {}", e))
123 })?;
124
125 let cache_dir = if let Some(custom_dir) = config_clone.cache_dir.as_deref() {
127 let custom_path = std::path::PathBuf::from(custom_dir);
128 if custom_path.is_absolute() {
129 let path_components: Vec<_> = custom_path
131 .components()
132 .filter(|c| !c.as_os_str().is_empty())
133 .collect();
134
135 if let Some(last_component) = path_components.last() {
136 base_cache_dir.join(last_component.as_os_str())
137 } else {
138 base_cache_dir
139 }
140 } else {
141 base_cache_dir.join(custom_dir)
143 }
144 } else {
145 base_cache_dir
146 };
147
148 std::fs::create_dir_all(&cache_dir).map_err(|e| {
150 crate::Error::Cache(format!("Failed to create cache directory: {}", e))
151 })?;
152
153 let cache_file_name = config_clone
155 .cache_file_name
156 .as_deref()
157 .unwrap_or("tauri_cache.json");
158 let cache_file_path = cache_dir.join(cache_file_name);
159
160 mobile::init_with_config(
161 app,
162 api,
163 cache_file_path,
164 config_clone.cleanup_interval.unwrap_or(60),
165 )?
166 };
167
168 app.manage(cache);
169 Ok(())
170 })
171 .build()
172}