refactor(providers): drop redundant cache invalidation in workbench

useConfigStore.updateConfigValue already invalidates the section cache internally; the 16 clearCache calls issued right after it were no-ops.
This commit is contained in:
LTbinglingfeng
2026-06-13 02:17:45 +08:00
Unverified
parent 5e7225da21
commit 297e29bcf3
+8 -26
View File
@@ -174,7 +174,6 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
const config = useConfigStore((s) => s.config); const config = useConfigStore((s) => s.config);
const fetchConfig = useConfigStore((s) => s.fetchConfig); const fetchConfig = useConfigStore((s) => s.fetchConfig);
const updateConfigValue = useConfigStore((s) => s.updateConfigValue); const updateConfigValue = useConfigStore((s) => s.updateConfigValue);
const clearCache = useConfigStore((s) => s.clearCache);
const isCacheValid = useConfigStore((s) => s.isCacheValid); const isCacheValid = useConfigStore((s) => s.isCacheValid);
const [isPending, setIsPending] = useState<boolean>(() => !isCacheValid()); const [isPending, setIsPending] = useState<boolean>(() => !isCacheValid());
@@ -202,15 +201,12 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
} }
if (vertexResult.status === 'fulfilled') { if (vertexResult.status === 'fulfilled') {
updateConfigValue('vertex-api-key', vertexResult.value || []); updateConfigValue('vertex-api-key', vertexResult.value || []);
clearCache('vertex-api-key');
} }
if (ampcodeResult.status === 'fulfilled') { if (ampcodeResult.status === 'fulfilled') {
updateConfigValue('ampcode', ampcodeResult.value); updateConfigValue('ampcode', ampcodeResult.value);
clearCache('ampcode');
} }
if (openaiResult.status === 'fulfilled') { if (openaiResult.status === 'fulfilled') {
updateConfigValue('openai-compatibility', openaiResult.value || []); updateConfigValue('openai-compatibility', openaiResult.value || []);
clearCache('openai-compatibility');
} }
setFetchedAt(new Date().toISOString()); setFetchedAt(new Date().toISOString());
} catch (err) { } catch (err) {
@@ -219,7 +215,7 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
setIsPending(false); setIsPending(false);
setIsFetching(false); setIsFetching(false);
} }
}, [clearCache, fetchConfig, updateConfigValue]); }, [fetchConfig, updateConfigValue]);
const refreshSnapshot = useCallback(() => { const refreshSnapshot = useCallback(() => {
setFetchedAt(new Date().toISOString()); setFetchedAt(new Date().toISOString());
@@ -275,45 +271,40 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
async (next: GeminiKeyConfig[]) => { async (next: GeminiKeyConfig[]) => {
await providersApi.saveGeminiKeys(next); await providersApi.saveGeminiKeys(next);
updateConfigValue('gemini-api-key', next); updateConfigValue('gemini-api-key', next);
clearCache('gemini-api-key');
}, },
[clearCache, updateConfigValue] [updateConfigValue]
); );
const persistCodexConfigs = useCallback( const persistCodexConfigs = useCallback(
async (next: ProviderKeyConfig[]) => { async (next: ProviderKeyConfig[]) => {
await providersApi.saveCodexConfigs(next); await providersApi.saveCodexConfigs(next);
updateConfigValue('codex-api-key', next); updateConfigValue('codex-api-key', next);
clearCache('codex-api-key');
}, },
[clearCache, updateConfigValue] [updateConfigValue]
); );
const persistClaudeConfigs = useCallback( const persistClaudeConfigs = useCallback(
async (next: ProviderKeyConfig[]) => { async (next: ProviderKeyConfig[]) => {
await providersApi.saveClaudeConfigs(next); await providersApi.saveClaudeConfigs(next);
updateConfigValue('claude-api-key', next); updateConfigValue('claude-api-key', next);
clearCache('claude-api-key');
}, },
[clearCache, updateConfigValue] [updateConfigValue]
); );
const persistVertexConfigs = useCallback( const persistVertexConfigs = useCallback(
async (next: ProviderKeyConfig[]) => { async (next: ProviderKeyConfig[]) => {
await providersApi.saveVertexConfigs(next); await providersApi.saveVertexConfigs(next);
updateConfigValue('vertex-api-key', next); updateConfigValue('vertex-api-key', next);
clearCache('vertex-api-key');
}, },
[clearCache, updateConfigValue] [updateConfigValue]
); );
const persistOpenAIConfigs = useCallback( const persistOpenAIConfigs = useCallback(
async (next: OpenAIProviderConfig[]) => { async (next: OpenAIProviderConfig[]) => {
await providersApi.saveOpenAIProviders(next); await providersApi.saveOpenAIProviders(next);
updateConfigValue('openai-compatibility', next); updateConfigValue('openai-compatibility', next);
clearCache('openai-compatibility');
}, },
[clearCache, updateConfigValue] [updateConfigValue]
); );
const createProvider = useCallback( const createProvider = useCallback(
@@ -418,27 +409,22 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
await providersApi.deleteGeminiKey(sel.apiKey, sel.baseUrl); await providersApi.deleteGeminiKey(sel.apiKey, sel.baseUrl);
const next = (config?.geminiApiKeys ?? []).filter((_, i) => i !== sel.index); const next = (config?.geminiApiKeys ?? []).filter((_, i) => i !== sel.index);
updateConfigValue('gemini-api-key', next); updateConfigValue('gemini-api-key', next);
clearCache('gemini-api-key');
} else if (sel.brand === 'codex') { } else if (sel.brand === 'codex') {
await providersApi.deleteCodexConfig(sel.apiKey, sel.baseUrl); await providersApi.deleteCodexConfig(sel.apiKey, sel.baseUrl);
const next = (config?.codexApiKeys ?? []).filter((_, i) => i !== sel.index); const next = (config?.codexApiKeys ?? []).filter((_, i) => i !== sel.index);
updateConfigValue('codex-api-key', next); updateConfigValue('codex-api-key', next);
clearCache('codex-api-key');
} else if (sel.brand === 'claude') { } else if (sel.brand === 'claude') {
await providersApi.deleteClaudeConfig(sel.apiKey, sel.baseUrl); await providersApi.deleteClaudeConfig(sel.apiKey, sel.baseUrl);
const next = (config?.claudeApiKeys ?? []).filter((_, i) => i !== sel.index); const next = (config?.claudeApiKeys ?? []).filter((_, i) => i !== sel.index);
updateConfigValue('claude-api-key', next); updateConfigValue('claude-api-key', next);
clearCache('claude-api-key');
} else if (sel.brand === 'vertex') { } else if (sel.brand === 'vertex') {
await providersApi.deleteVertexConfig(sel.apiKey, sel.baseUrl); await providersApi.deleteVertexConfig(sel.apiKey, sel.baseUrl);
const next = (config?.vertexApiKeys ?? []).filter((_, i) => i !== sel.index); const next = (config?.vertexApiKeys ?? []).filter((_, i) => i !== sel.index);
updateConfigValue('vertex-api-key', next); updateConfigValue('vertex-api-key', next);
clearCache('vertex-api-key');
} else if (sel.brand === 'openaiCompatibility') { } else if (sel.brand === 'openaiCompatibility') {
await providersApi.deleteOpenAIProvider(sel.index); await providersApi.deleteOpenAIProvider(sel.index);
const next = (config?.openaiCompatibility ?? []).filter((_, i) => i !== sel.index); const next = (config?.openaiCompatibility ?? []).filter((_, i) => i !== sel.index);
updateConfigValue('openai-compatibility', next); updateConfigValue('openai-compatibility', next);
clearCache('openai-compatibility');
} else if (sel.brand === 'ampcode') { } else if (sel.brand === 'ampcode') {
await Promise.allSettled([ await Promise.allSettled([
ampcodeApi.clearUpstreamUrl(), ampcodeApi.clearUpstreamUrl(),
@@ -446,14 +432,13 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
ampcodeApi.clearModelMappings(), ampcodeApi.clearModelMappings(),
]); ]);
updateConfigValue('ampcode', {}); updateConfigValue('ampcode', {});
clearCache('ampcode');
} }
refreshSnapshot(); refreshSnapshot();
} finally { } finally {
setMutating(false); setMutating(false);
} }
}, },
[clearCache, config, refreshSnapshot, updateConfigValue] [config, refreshSnapshot, updateConfigValue]
); );
const toggleDisabled = useCallback( const toggleDisabled = useCallback(
@@ -495,7 +480,6 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
if (current) { if (current) {
list[idx] = { ...current, disabled }; list[idx] = { ...current, disabled };
updateConfigValue('openai-compatibility', list); updateConfigValue('openai-compatibility', list);
clearCache('openai-compatibility');
} }
} else if (brand === 'ampcode') { } else if (brand === 'ampcode') {
/* ampcode toggle 不支持,跳过 */ /* ampcode toggle 不支持,跳过 */
@@ -506,7 +490,6 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
} }
}, },
[ [
clearCache,
config, config,
persistClaudeConfigs, persistClaudeConfigs,
persistCodexConfigs, persistCodexConfigs,
@@ -551,13 +534,12 @@ export function useProviderWorkbench(): UseProviderWorkbenchResult {
await ampcodeApi.updateForceModelMappings(next.forceModelMappings === true); await ampcodeApi.updateForceModelMappings(next.forceModelMappings === true);
updateConfigValue('ampcode', next); updateConfigValue('ampcode', next);
clearCache('ampcode');
refreshSnapshot(); refreshSnapshot();
} finally { } finally {
setMutating(false); setMutating(false);
} }
}, },
[clearCache, refreshSnapshot, updateConfigValue] [updateConfigValue, refreshSnapshot]
); );
return { return {