mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat: Implement caching mechanism with Cache struct and update related commands
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
// use crate::utils::logging::Type;
|
|
||||||
// use crate::{logging, singleton};
|
|
||||||
use crate::singleton;
|
use crate::singleton;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
@@ -12,13 +10,13 @@ pub struct CacheEntry {
|
|||||||
pub expires_at: Instant,
|
pub expires_at: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ProxyRequestCache {
|
pub struct Cache {
|
||||||
pub map: DashMap<String, Arc<OnceCell<Box<CacheEntry>>>>,
|
pub map: DashMap<String, Arc<OnceCell<Box<CacheEntry>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProxyRequestCache {
|
impl Cache {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
ProxyRequestCache {
|
Cache {
|
||||||
map: DashMap::new(),
|
map: DashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,4 +97,4 @@ impl ProxyRequestCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use singleton macro
|
// Use singleton macro
|
||||||
singleton!(ProxyRequestCache, INSTANCE);
|
singleton!(Cache, INSTANCE);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use super::CmdResult;
|
use super::CmdResult;
|
||||||
use crate::{
|
use crate::{
|
||||||
|
cache::Cache,
|
||||||
config::Config,
|
config::Config,
|
||||||
core::{CoreManager, handle},
|
core::{CoreManager, handle},
|
||||||
};
|
};
|
||||||
@@ -8,7 +9,6 @@ use crate::{
|
|||||||
feat,
|
feat,
|
||||||
ipc::{self, IpcManager},
|
ipc::{self, IpcManager},
|
||||||
logging,
|
logging,
|
||||||
state::proxy::ProxyRequestCache,
|
|
||||||
utils::logging::Type,
|
utils::logging::Type,
|
||||||
wrap_err,
|
wrap_err,
|
||||||
};
|
};
|
||||||
@@ -317,8 +317,8 @@ pub async fn get_clash_version() -> CmdResult<serde_json::Value> {
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_clash_config() -> CmdResult<serde_json::Value> {
|
pub async fn get_clash_config() -> CmdResult<serde_json::Value> {
|
||||||
let manager = IpcManager::global();
|
let manager = IpcManager::global();
|
||||||
let cache = ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = ProxyRequestCache::make_key("clash_config", "default");
|
let key = Cache::make_key("clash_config", "default");
|
||||||
let value = cache
|
let value = cache
|
||||||
.get_or_fetch(key, CONFIG_REFRESH_INTERVAL, || async {
|
.get_or_fetch(key, CONFIG_REFRESH_INTERVAL, || async {
|
||||||
manager.get_config().await.unwrap_or_else(|e| {
|
manager.get_config().await.unwrap_or_else(|e| {
|
||||||
@@ -333,8 +333,8 @@ pub async fn get_clash_config() -> CmdResult<serde_json::Value> {
|
|||||||
/// 强制刷新Clash配置缓存
|
/// 强制刷新Clash配置缓存
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn force_refresh_clash_config() -> CmdResult<serde_json::Value> {
|
pub async fn force_refresh_clash_config() -> CmdResult<serde_json::Value> {
|
||||||
let cache = ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = ProxyRequestCache::make_key("clash_config", "default");
|
let key = Cache::make_key("clash_config", "default");
|
||||||
cache.map.remove(&key);
|
cache.map.remove(&key);
|
||||||
get_clash_config().await
|
get_clash_config().await
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ use tauri::Emitter;
|
|||||||
|
|
||||||
use super::CmdResult;
|
use super::CmdResult;
|
||||||
use crate::{
|
use crate::{
|
||||||
|
cache::Cache,
|
||||||
core::{handle::Handle, tray::Tray},
|
core::{handle::Handle, tray::Tray},
|
||||||
ipc::IpcManager,
|
ipc::IpcManager,
|
||||||
logging,
|
logging,
|
||||||
state::proxy::ProxyRequestCache,
|
|
||||||
utils::logging::Type,
|
utils::logging::Type,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -15,8 +15,8 @@ const PROVIDERS_REFRESH_INTERVAL: Duration = Duration::from_secs(60);
|
|||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
||||||
let cache = ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = ProxyRequestCache::make_key("proxies", "default");
|
let key = Cache::make_key("proxies", "default");
|
||||||
let value = cache
|
let value = cache
|
||||||
.get_or_fetch(key, PROXIES_REFRESH_INTERVAL, || async {
|
.get_or_fetch(key, PROXIES_REFRESH_INTERVAL, || async {
|
||||||
let manager = IpcManager::global();
|
let manager = IpcManager::global();
|
||||||
@@ -32,16 +32,16 @@ pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
|||||||
/// 强制刷新代理缓存用于profile切换
|
/// 强制刷新代理缓存用于profile切换
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn force_refresh_proxies() -> CmdResult<serde_json::Value> {
|
pub async fn force_refresh_proxies() -> CmdResult<serde_json::Value> {
|
||||||
let cache = ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = ProxyRequestCache::make_key("proxies", "default");
|
let key = Cache::make_key("proxies", "default");
|
||||||
cache.map.remove(&key);
|
cache.map.remove(&key);
|
||||||
get_proxies().await
|
get_proxies().await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
|
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
|
||||||
let cache = ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = ProxyRequestCache::make_key("providers", "default");
|
let key = Cache::make_key("providers", "default");
|
||||||
let value = cache
|
let value = cache
|
||||||
.get_or_fetch(key, PROVIDERS_REFRESH_INTERVAL, || async {
|
.get_or_fetch(key, PROVIDERS_REFRESH_INTERVAL, || async {
|
||||||
let manager = IpcManager::global();
|
let manager = IpcManager::global();
|
||||||
@@ -85,8 +85,8 @@ pub async fn update_proxy_and_sync(group: String, proxy: String) -> CmdResult<()
|
|||||||
proxy
|
proxy
|
||||||
);
|
);
|
||||||
|
|
||||||
let cache = crate::state::proxy::ProxyRequestCache::global();
|
let cache = Cache::global();
|
||||||
let key = crate::state::proxy::ProxyRequestCache::make_key("proxies", "default");
|
let key = Cache::make_key("proxies", "default");
|
||||||
cache.map.remove(&key);
|
cache.map.remove(&key);
|
||||||
|
|
||||||
if let Err(e) = Tray::global().update_menu().await {
|
if let Err(e) = Tray::global().update_menu().await {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
#![recursion_limit = "512"]
|
#![recursion_limit = "512"]
|
||||||
|
|
||||||
|
mod cache;
|
||||||
mod cmd;
|
mod cmd;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod core;
|
mod core;
|
||||||
@@ -9,7 +10,6 @@ mod feat;
|
|||||||
mod ipc;
|
mod ipc;
|
||||||
mod module;
|
mod module;
|
||||||
mod process;
|
mod process;
|
||||||
mod state;
|
|
||||||
mod utils;
|
mod utils;
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
use crate::utils::window_manager::WindowManager;
|
use crate::utils::window_manager::WindowManager;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
cache::Cache,
|
||||||
config::Config,
|
config::Config,
|
||||||
core::{handle, timer::Timer, tray::Tray},
|
core::{handle, timer::Timer, tray::Tray},
|
||||||
log_err, logging,
|
log_err, logging,
|
||||||
process::AsyncHandler,
|
process::AsyncHandler,
|
||||||
state::proxy::ProxyRequestCache,
|
|
||||||
utils::logging::Type,
|
utils::logging::Type,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -183,7 +183,7 @@ pub async fn entry_lightweight_mode() -> bool {
|
|||||||
// 回到 In
|
// 回到 In
|
||||||
set_state(LightweightState::In);
|
set_state(LightweightState::In);
|
||||||
|
|
||||||
ProxyRequestCache::global().clean_default_keys();
|
Cache::global().clean_default_keys();
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
|
|
||||||
// https://tauri.app/develop/state-management/#do-you-need-arc
|
|
||||||
|
|
||||||
pub mod proxy;
|
|
||||||
Reference in New Issue
Block a user