feat: Implement caching mechanism with Cache struct and update related commands

This commit is contained in:
Tunglies
2025-09-17 19:37:42 +08:00
parent 1787d5372e
commit 6724f1ae35
6 changed files with 21 additions and 27 deletions

View File

@@ -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<String, Arc<OnceCell<Box<CacheEntry>>>>,
}
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);

View File

@@ -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<serde_json::Value> {
#[tauri::command]
pub async fn get_clash_config() -> CmdResult<serde_json::Value> {
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<serde_json::Value> {
/// 强制刷新Clash配置缓存
#[tauri::command]
pub async fn force_refresh_clash_config() -> CmdResult<serde_json::Value> {
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
}

View File

@@ -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<serde_json::Value> {
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<serde_json::Value> {
/// 强制刷新代理缓存用于profile切换
#[tauri::command]
pub async fn force_refresh_proxies() -> CmdResult<serde_json::Value> {
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<serde_json::Value> {
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 {

View File

@@ -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;

View File

@@ -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
}

View File

@@ -1,4 +0,0 @@
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
// https://tauri.app/develop/state-management/#do-you-need-arc
pub mod proxy;