Skip to main content

tauri_plugin_redb_cache/
lib.rs

1//! # Tauri Plugin Redb Cache
2//!
3//! A Tauri plugin for HTTP and image caching using Redb with LRU memory cache and compression.
4//!
5//! ## Features
6//! - Two-tier caching: LRU memory cache + Redb persistent storage
7//! - Automatic Zlib compression for large data (>1KB)
8//! - Separate tables for HTTP responses and images
9//! - Configurable TTL, memory cache size, and compression threshold
10//! - Background cleanup task for expired entries
11//!
12//! ## Usage
13//! ```rust
14//! fn main() {
15//!     tauri::Builder::default()
16//!         .plugin(tauri_plugin_redb_cache::Builder::new().build())
17//!         .run(tauri::generate_context!())
18//!         .unwrap();
19//! }
20//! ```
21
22mod cache;
23mod commands;
24mod config;
25
26pub use config::{CacheConfig, Builder};
27
28use std::sync::OnceLock;
29use tauri::{
30    plugin::{self, TauriPlugin},
31    Runtime,
32};
33
34pub(crate) static CONFIG: OnceLock<CacheConfig> = OnceLock::new();
35
36/// Get the plugin configuration
37pub fn get_config() -> &'static CacheConfig {
38    CONFIG.get_or_init(CacheConfig::default)
39}
40
41/// Initialize the plugin with the given app handle
42pub fn init<R: Runtime>() -> TauriPlugin<R> {
43    Builder::new().build()
44}
45
46impl Builder {
47    /// Build the plugin
48    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
49        // Store config
50        let _ = CONFIG.set(self.config.clone());
51
52        plugin::Builder::new("redb-cache")
53            .invoke_handler(tauri::generate_handler![
54                commands::cache_get,
55                commands::cache_set,
56                commands::cache_remove,
57                commands::cache_clear,
58                commands::cache_clean_expired,
59                commands::cache_info,
60                commands::cache_list,
61                commands::image_cache_get,
62                commands::image_cache_set,
63                commands::image_cache_remove,
64                commands::image_cache_clean_expired,
65                commands::image_cache_clear,
66                commands::image_cache_info,
67                commands::image_cache_list,
68            ])
69            .setup(|app, _api| {
70                cache::init_cache(app)?;
71                Ok(())
72            })
73            .build()
74    }
75}