From 6724f1ae35a4926ab08b1a73a83b29a09cb15aaf Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Wed, 17 Sep 2025 19:37:42 +0800 Subject: [PATCH] feat: Implement caching mechanism with Cache struct and update related commands --- src-tauri/src/{state/proxy.rs => cache/mod.rs} | 10 ++++------ src-tauri/src/cmd/clash.rs | 10 +++++----- src-tauri/src/cmd/proxy.rs | 18 +++++++++--------- src-tauri/src/lib.rs | 2 +- src-tauri/src/module/lightweight.rs | 4 ++-- src-tauri/src/state/mod.rs | 4 ---- 6 files changed, 21 insertions(+), 27 deletions(-) rename src-tauri/src/{state/proxy.rs => cache/mod.rs} (94%) delete mode 100644 src-tauri/src/state/mod.rs diff --git a/src-tauri/src/state/proxy.rs b/src-tauri/src/cache/mod.rs similarity index 94% rename from src-tauri/src/state/proxy.rs rename to src-tauri/src/cache/mod.rs index c0c139b35..3b8109e39 100644 --- a/src-tauri/src/state/proxy.rs +++ b/src-tauri/src/cache/mod.rs @@ -1,5 +1,3 @@ -// use crate::utils::logging::Type; -// use crate::{logging, singleton}; use crate::singleton; use dashmap::DashMap; use serde_json::Value; @@ -12,13 +10,13 @@ pub struct CacheEntry { pub expires_at: Instant, } -pub struct ProxyRequestCache { +pub struct Cache { pub map: DashMap>>>, } -impl ProxyRequestCache { +impl Cache { fn new() -> Self { - ProxyRequestCache { + Cache { map: DashMap::new(), } } @@ -99,4 +97,4 @@ impl ProxyRequestCache { } // Use singleton macro -singleton!(ProxyRequestCache, INSTANCE); +singleton!(Cache, INSTANCE); diff --git a/src-tauri/src/cmd/clash.rs b/src-tauri/src/cmd/clash.rs index ecd8de153..01f23deb2 100644 --- a/src-tauri/src/cmd/clash.rs +++ b/src-tauri/src/cmd/clash.rs @@ -1,5 +1,6 @@ use super::CmdResult; use crate::{ + cache::Cache, config::Config, core::{CoreManager, handle}, }; @@ -8,7 +9,6 @@ use crate::{ feat, ipc::{self, IpcManager}, logging, - state::proxy::ProxyRequestCache, utils::logging::Type, wrap_err, }; @@ -317,8 +317,8 @@ pub async fn get_clash_version() -> CmdResult { #[tauri::command] pub async fn get_clash_config() -> CmdResult { let manager = IpcManager::global(); - let cache = ProxyRequestCache::global(); - let key = ProxyRequestCache::make_key("clash_config", "default"); + let cache = Cache::global(); + let key = Cache::make_key("clash_config", "default"); let value = cache .get_or_fetch(key, CONFIG_REFRESH_INTERVAL, || async { manager.get_config().await.unwrap_or_else(|e| { @@ -333,8 +333,8 @@ pub async fn get_clash_config() -> CmdResult { /// 强制刷新Clash配置缓存 #[tauri::command] pub async fn force_refresh_clash_config() -> CmdResult { - let cache = ProxyRequestCache::global(); - let key = ProxyRequestCache::make_key("clash_config", "default"); + let cache = Cache::global(); + let key = Cache::make_key("clash_config", "default"); cache.map.remove(&key); get_clash_config().await } diff --git a/src-tauri/src/cmd/proxy.rs b/src-tauri/src/cmd/proxy.rs index f2f9a2a95..f29bcc3e6 100644 --- a/src-tauri/src/cmd/proxy.rs +++ b/src-tauri/src/cmd/proxy.rs @@ -2,10 +2,10 @@ use tauri::Emitter; use super::CmdResult; use crate::{ + cache::Cache, core::{handle::Handle, tray::Tray}, ipc::IpcManager, logging, - state::proxy::ProxyRequestCache, utils::logging::Type, }; use std::time::Duration; @@ -15,8 +15,8 @@ const PROVIDERS_REFRESH_INTERVAL: Duration = Duration::from_secs(60); #[tauri::command] pub async fn get_proxies() -> CmdResult { - let cache = ProxyRequestCache::global(); - let key = ProxyRequestCache::make_key("proxies", "default"); + let cache = Cache::global(); + let key = Cache::make_key("proxies", "default"); let value = cache .get_or_fetch(key, PROXIES_REFRESH_INTERVAL, || async { let manager = IpcManager::global(); @@ -32,16 +32,16 @@ pub async fn get_proxies() -> CmdResult { /// 强制刷新代理缓存用于profile切换 #[tauri::command] pub async fn force_refresh_proxies() -> CmdResult { - let cache = ProxyRequestCache::global(); - let key = ProxyRequestCache::make_key("proxies", "default"); + let cache = Cache::global(); + let key = Cache::make_key("proxies", "default"); cache.map.remove(&key); get_proxies().await } #[tauri::command] pub async fn get_providers_proxies() -> CmdResult { - let cache = ProxyRequestCache::global(); - let key = ProxyRequestCache::make_key("providers", "default"); + let cache = Cache::global(); + let key = Cache::make_key("providers", "default"); let value = cache .get_or_fetch(key, PROVIDERS_REFRESH_INTERVAL, || async { let manager = IpcManager::global(); @@ -85,8 +85,8 @@ pub async fn update_proxy_and_sync(group: String, proxy: String) -> CmdResult<() proxy ); - let cache = crate::state::proxy::ProxyRequestCache::global(); - let key = crate::state::proxy::ProxyRequestCache::make_key("proxies", "default"); + let cache = Cache::global(); + let key = Cache::make_key("proxies", "default"); cache.map.remove(&key); if let Err(e) = Tray::global().update_menu().await { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a7412856b..13590e1b4 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,6 +1,7 @@ #![allow(non_snake_case)] #![recursion_limit = "512"] +mod cache; mod cmd; pub mod config; mod core; @@ -9,7 +10,6 @@ mod feat; mod ipc; mod module; mod process; -mod state; mod utils; #[cfg(target_os = "macos")] use crate::utils::window_manager::WindowManager; diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index df57785d1..941e1d845 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -1,9 +1,9 @@ use crate::{ + cache::Cache, config::Config, core::{handle, timer::Timer, tray::Tray}, log_err, logging, process::AsyncHandler, - state::proxy::ProxyRequestCache, utils::logging::Type, }; @@ -183,7 +183,7 @@ pub async fn entry_lightweight_mode() -> bool { // 回到 In set_state(LightweightState::In); - ProxyRequestCache::global().clean_default_keys(); + Cache::global().clean_default_keys(); true } diff --git a/src-tauri/src/state/mod.rs b/src-tauri/src/state/mod.rs deleted file mode 100644 index 94f453c3a..000000000 --- a/src-tauri/src/state/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -// Tauri Manager 会进行 Arc 管理,无需额外 Arc -// https://tauri.app/develop/state-management/#do-you-need-arc - -pub mod proxy;