refactor: 将上游模型缓存从前端迁移到后端 Redis

- 后端:定时任务刷新时将上游模型写入 Redis 缓存
- 后端:provider_query 优先从缓存读取,支持 force_refresh 参数
- 后端:auto_fetch_models 开启时改为同步获取,确保前端能立即看到数据
- 前端:移除本地缓存,只保留并发请求去重逻辑
- 前端:KeyAllowedModelsEditDialog 添加刷新上游模型按钮
This commit is contained in:
fawney19
2026-01-16 19:10:59 +08:00
parent b2d08f964d
commit 019e148ddb
7 changed files with 148 additions and 112 deletions

View File

@@ -257,7 +257,7 @@ const emit = defineEmits<{
'changed': []
}>()
const { fetchModels: fetchCachedModels, clearCache, getCachedModels } = useUpstreamModelsCache()
const { fetchModels: fetchCachedModels } = useUpstreamModelsCache()
const { error: showError, success } = useToast()
const { confirmWarning } = useConfirm()
@@ -633,24 +633,8 @@ async function loadData() {
// 同步全局模型选择状态
syncGlobalModelSelection()
// 检查缓存
const cachedModels = getCachedModels(props.providerId)
if (cachedModels && cachedModels.length > 0) {
upstreamModels.value = cachedModels
upstreamModelsLoaded.value = true
// 同步上游模型选择状态
syncUpstreamModelSelection()
// 有多个分组时全部折叠
const allGroups = new Set(['global'])
for (const model of cachedModels) {
if (model.api_format) {
allGroups.add(model.api_format)
}
}
collapsedGroups.value = allGroups
} else {
collapsedGroups.value = new Set()
}
// 初始折叠状态
collapsedGroups.value = new Set()
}
// 加载全局模型列表
@@ -677,13 +661,9 @@ async function loadExistingModels() {
// 从提供商获取模型
async function fetchUpstreamModels(forceRefresh = false) {
if (forceRefresh) {
clearCache(props.providerId)
}
try {
fetchingUpstreamModels.value = true
const result = await fetchCachedModels(props.providerId, forceRefresh)
const result = await fetchCachedModels(props.providerId, undefined, forceRefresh)
if (result) {
if (result.error) {
showError(result.error, '错误')

View File

@@ -40,10 +40,23 @@
>
已选 {{ selectedModels.length }}
</span>
<Loader2
v-if="fetchingUpstreamModels"
class="w-4 h-4 animate-spin text-muted-foreground shrink-0"
/>
<!-- 刷新上游模型按钮 -->
<button
type="button"
class="h-6 w-6 flex items-center justify-center rounded hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
:disabled="fetchingUpstreamModels"
title="刷新上游模型"
@click="refreshUpstreamModels"
>
<RefreshCw
v-if="!fetchingUpstreamModels"
class="w-3.5 h-3.5"
/>
<Loader2
v-else
class="w-3.5 h-3.5 animate-spin"
/>
</button>
</div>
<!-- 分组列表 -->
@@ -318,7 +331,8 @@ import {
Check,
ChevronDown,
Lock,
LockOpen
LockOpen,
RefreshCw
} from 'lucide-vue-next'
import { Dialog, Button, Input } from '@/components/ui'
import { useToast } from '@/composables/useToast'
@@ -627,11 +641,11 @@ async function loadGlobalModels() {
}
// 从提供商获取模型(使用缓存)
async function fetchUpstreamModels() {
async function fetchUpstreamModels(forceRefresh = false) {
if (!props.providerId || !props.apiKey) return
try {
fetchingUpstreamModels.value = true
const result = await fetchCachedModels(props.providerId, props.apiKey.id)
const result = await fetchCachedModels(props.providerId, props.apiKey.id, forceRefresh)
if (loadingCancelled) return
if (result.models.length > 0) {
upstreamModels.value = result.models
@@ -647,6 +661,14 @@ async function fetchUpstreamModels() {
}
}
// 手动刷新上游模型(强制跳过缓存)
async function refreshUpstreamModels() {
await fetchUpstreamModels(true)
if (upstreamModels.value.length > 0) {
success('上游模型已刷新')
}
}
// 解析 allowed_models
function parseAllowedModels(allowed: AllowedModels): string[] {
if (allowed === null || allowed === undefined) {