topfans/frontend/utils/handlers/othersHandler.js
2026-07-29 16:03:33 +08:00

79 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// frontend/utils/handlers/othersHandler.js
// 兜底分类:未匹配其他规则的 key且不在黑名单
// 注意:草稿 keys无论 currentUid / otherUid / legacy**全部**归 draftHandler
// 否则会双重计入 totalBytes 且强警告无法生效
//
// 安全白名单reviewer P0以下 key 属于其他模块的工作流关键状态,
// 严禁被 othersHandler 清理:
import { isProtectedKey } from '@/utils/cacheManager'
import { getAllBaseKeys } from '@/utils/draftStorage'
const PRELOAD_PREFIX = 'preload:'
const PROGRESS_PREFIX = 'progress_'
const GUIDE_PREFIX = 'guide_'
const DRAFT_BASE_KEYS = getAllBaseKeys()
// 工作流关键 key 白名单reviewer P0 修正:原"全收"策略会破坏这些)
const WORKFLOW_CRITICAL_KEYS = new Set([
'generation_flow_payload',
'generation_request_data',
'craft_selected_index',
'__package_info__',
'UNI_ADMIN_UPGRADE_CENTER_LOCAL_FILE_PATH',
])
function isClaimed(key) {
if (isProtectedKey(key)) return false
if (WORKFLOW_CRITICAL_KEYS.has(key)) return false
if (key.startsWith('topfans_pending_actions_')) return false
if (key.startsWith(PRELOAD_PREFIX)) return false
if (key.startsWith(PROGRESS_PREFIX)) return false
if (key.startsWith(GUIDE_PREFIX)) return false
if (DRAFT_BASE_KEYS.some((base) => key === base || key.startsWith(`${base}_`))) return false
return true
}
export default {
id: 'others',
label: '其他业务缓存',
description: '未归类的少量业务数据',
warning: false,
async computeSize() {
try {
const info = uni.getStorageInfoSync()
const keys = (info.keys || []).filter(isClaimed)
let bytes = 0
for (const k of keys) {
try { const v = uni.getStorageSync(k); if (v != null) bytes += JSON.stringify(v).length } catch (e) {}
}
return { sizeBytes: bytes, keyCount: keys.length }
} catch (e) { return { sizeBytes: 0, keyCount: 0 } }
},
async clean() {
// Top-level errors propagate to cacheManager (per spec §6.1)
const info = uni.getStorageInfoSync()
const keys = (info.keys || []).filter(isClaimed)
let freed = 0
let count = 0
for (const k of keys) {
let valueBytes = 0
try {
const v = uni.getStorageSync(k)
if (v != null) valueBytes = JSON.stringify(v).length
} catch (e) {
console.warn('[othersHandler] read failed:', k, e.message)
continue // skip this key, don't try to delete
}
try {
uni.removeStorageSync(k)
freed += valueBytes
count++
} catch (e) {
console.warn('[othersHandler] remove failed:', k, e.message)
// don't count as freed
}
}
return { freedBytes: freed, keyCount: count }
},
}