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

52 lines
1.5 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/preloadHandler.js
// preload 缓存 handler简单型但跨用户清理 + invalidateAll
// 跨用户:删除所有 preload:xxx:* 不依赖 currentUid
// 内存清理:由 cacheManager.cleanCategory 触发 invalidateAll
const PREFIX = 'preload:'
function listAllPreloadKeys() {
try {
const info = uni.getStorageInfoSync()
return (info.keys || []).filter((k) => k.startsWith(PREFIX))
} catch (e) { return [] }
}
export default {
id: 'preload',
label: '预加载数据缓存',
description: '列表页提前拉取的数据',
warning: false,
async computeSize() {
const keys = listAllPreloadKeys()
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 }
},
async clean() {
const keys = listAllPreloadKeys()
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('[preloadHandler] read failed:', k, e.message)
continue
}
try {
uni.removeStorageSync(k)
freed += valueBytes
count++
} catch (e) {
console.warn('[preloadHandler] remove failed:', k, e.message)
}
}
return { freedBytes: freed, keyCount: count }
},
}