feat: enhance proxy management with caching and refresh logic

This commit is contained in:
Tunglies
2025-06-30 20:14:04 +08:00
parent 4435a5aee4
commit 18ef7f0272
9 changed files with 113 additions and 123 deletions

View File

@@ -8,6 +8,7 @@ import {
deleteConnection,
getGroupProxyDelays,
} from "@/services/api";
import { forceRefreshProxies } from "@/services/cmds";
import { useProfiles } from "@/hooks/use-profiles";
import { useVerge } from "@/hooks/use-verge";
import { BaseEmpty } from "../base";
@@ -341,6 +342,9 @@ export const ProxyGroups = (props: Props) => {
const { name, now } = group;
await updateProxy(name, proxy.name);
await forceRefreshProxies();
onProxies();
// 断开连接

View File

@@ -3,6 +3,7 @@ import {
getProfiles,
patchProfile,
patchProfilesConfig,
forceRefreshProxies,
} from "@/services/cmds";
import { getProxies, updateProxy } from "@/services/api";
@@ -128,6 +129,9 @@ export const useProfiles = () => {
await patchProfile(profileData.current!, { selected: newSelected });
console.log("[ActivateSelected] 代理选择配置保存成功");
// 切换节点后强制刷新后端缓存
await forceRefreshProxies();
setTimeout(() => {
mutate("getProxies", getProxies());
}, 100);

View File

@@ -101,33 +101,19 @@ export const AppDataProvider = ({
lastProfileId = newProfileId;
lastUpdateTime = now;
setTimeout(async () => {
try {
console.log("[AppDataProvider] 强制刷新代理缓存");
const refreshPromise = Promise.race([
forceRefreshProxies(),
new Promise((_, reject) =>
setTimeout(
() => reject(new Error("forceRefreshProxies timeout")),
8000,
),
),
]);
await refreshPromise;
console.log("[AppDataProvider] 刷新前端代理数据");
await refreshProxy();
console.log("[AppDataProvider] Profile切换的代理数据刷新完成");
} catch (error) {
console.error("[AppDataProvider] 强制刷新代理缓存失败:", error);
refreshProxy().catch((e) =>
console.warn("[AppDataProvider] 普通刷新也失败:", e),
);
}
setTimeout(() => {
// 先执行 forceRefreshProxies完成后稍延迟再刷新前端数据避免页面一直 loading
forceRefreshProxies()
.catch((e) =>
console.warn("[AppDataProvider] forceRefreshProxies 失败:", e),
)
.finally(() => {
setTimeout(() => {
refreshProxy().catch((e) =>
console.warn("[AppDataProvider] 普通刷新也失败:", e),
);
}, 200); // 200ms 延迟,保证后端缓存已清理
});
}, 0);
});