缓存问题修复

This commit is contained in:
ZheFox
2026-07-16 18:55:28 +08:00
parent 715a5ed626
commit f009fb73c3
5 changed files with 148 additions and 6 deletions
@@ -1020,6 +1020,7 @@ import {
getCodexResetCreditAvailableCount as getCodexResetCreditAvailableCountFromSnapshot,
getVisibleCodexResetCreditItems as getVisibleCodexResetCreditItemsFromSnapshot,
mergeCodexQuotaDisplays,
shouldRefreshMissingCodexResetCredits,
} from './codex-reset-credit-display'
// 扩展端点类型,包含密钥列表
@@ -2443,8 +2444,12 @@ function shouldAutoRefreshCodexQuota(): boolean {
if (isTokenExpiringSoon(key, now)) return true
// 旧缓存可能已有普通配额但没有 reset-credit 数据,仍需补拉一次
if (!hasCodexQuotaDisplayData(key) || getCodexResetCreditAvailableCount(key) === null) {
// 旧缓存缺少 reset-credit 数据时补拉;近期已检查过则等待缓存过期
if (!hasCodexQuotaDisplayData(key) || shouldRefreshMissingCodexResetCredits(
getCodexResetCreditsDisplay(key),
now,
AUTO_QUOTA_REFRESH_STALE_SECONDS,
)) {
return true
}
// 配额数据超过 5 分钟未更新,也触发刷新
@@ -7,6 +7,7 @@ import {
getCodexResetCreditAvailableCount,
getVisibleCodexResetCreditItems,
mergeCodexQuotaDisplays,
shouldRefreshMissingCodexResetCredits,
} from '@/features/providers/components/codex-reset-credit-display'
import type { QuotaResetCreditsSnapshot } from '@/api/endpoints/types'
@@ -59,6 +60,21 @@ describe('codex reset credit display helpers', () => {
expect(getVisibleCodexResetCreditItems(snapshot, 1_700_000_000)).toEqual([])
})
it('retries missing reset credits only when the cached detail check is stale', () => {
expect(shouldRefreshMissingCodexResetCredits(null, 1_700_000_000)).toBe(true)
expect(shouldRefreshMissingCodexResetCredits({
detail_status: 'failed',
updated_at: 1_699_999_900,
}, 1_700_000_000)).toBe(false)
expect(shouldRefreshMissingCodexResetCredits({
detail_status: 'failed',
updated_at: 1_699_999_600,
}, 1_700_000_000)).toBe(true)
expect(shouldRefreshMissingCodexResetCredits({
available_count: 0,
}, 1_700_000_000)).toBe(false)
})
it('sorts available detail items by remaining time and labels visible items with short ordinal keys', () => {
const snapshot: QuotaResetCreditsSnapshot = {
available_count: 7,
@@ -57,6 +57,18 @@ export function getCodexResetCreditAvailableCount(
return typeof count === 'number' && Number.isFinite(count) && count >= 0 ? count : null
}
export function shouldRefreshMissingCodexResetCredits(
snapshot: QuotaResetCreditsSnapshot | null | undefined,
nowUnixSecs = Math.floor(Date.now() / 1000),
staleSeconds = 5 * 60,
): boolean {
if (getCodexResetCreditAvailableCount(snapshot) !== null) return false
const updatedAt = Number(snapshot?.updated_at)
if (!Number.isFinite(updatedAt)) return true
return (nowUnixSecs - updatedAt) > staleSeconds
}
export function formatCodexResetCreditCount(count: number | null | undefined): string {
return `共 ${count ?? 0} 次机会`
}