feat: 优化 IDBClear 方法,支持保留指定键的缓存项

This commit is contained in:
kuaifan 2026-01-04 01:40:13 +00:00
parent 501ff21e55
commit 0d8e10b60e
2 changed files with 41 additions and 24 deletions

View File

@ -1685,8 +1685,27 @@ const timezone = require("dayjs/plugin/timezone");
return localforage.removeItem(key)
},
IDBClear() {
return localforage.clear()
/**
* 清除缓存
* @param {string[]} [keysToKeep] - 可选需要保留的 key 数组
* @returns {Promise<void>}
*/
async IDBClear(keysToKeep = []) {
if (!keysToKeep || !keysToKeep.length) {
return localforage.clear();
}
const cached = {};
await Promise.all(
keysToKeep.map(async key => {
cached[key] = await this.IDBValue(key);
})
);
await localforage.clear();
await Promise.all(
Object.entries(cached)
.filter(([, value]) => value !== null && value !== undefined)
.map(([key, value]) => this.IDBSet(key, value))
);
},
IDBValue(key) {

View File

@ -1127,30 +1127,28 @@ export default {
);
// localForage
const cacheItems = {
clientId: await $A.IDBString("clientId"),
cacheServerUrl: await $A.IDBString("cacheServerUrl"),
cacheCalendarView: await $A.IDBString("cacheCalendarView"),
cacheProjectParameter: await $A.IDBArray("cacheProjectParameter"),
cacheLoginEmail: await $A.IDBString("cacheLoginEmail"),
cacheFileSort: await $A.IDBJson("cacheFileSort"),
cacheTranslationLanguage: await $A.IDBString("cacheTranslationLanguage"),
cacheTranscriptionLanguage: await $A.IDBString("cacheTranscriptionLanguage"),
cacheTranslations: await $A.IDBArray("cacheTranslations"),
cacheEmojis: await $A.IDBArray("cacheEmojis"),
userInfo: await $A.IDBJson("userInfo"),
mcpServerStatus: await $A.IDBJson("mcpServerStatus"),
cacheVersion: state.cacheVersion,
};
await $A.IDBClear();
await Promise.all(
Object.entries(cacheItems).map(([key, value]) =>
$A.IDBSet(key, value)
)
);
const keysToKeep = [
'clientId',
'cacheServerUrl',
'cacheCalendarView',
'cacheProjectParameter',
'cacheLoginEmail',
'cacheFileSort',
'cacheTranslationLanguage',
'cacheTranscriptionLanguage',
'cacheTranslations',
'cacheEmojis',
'userInfo',
'mcpServerStatus',
'aiAssistant.model',
'aiAssistant.sessions',
];
await $A.IDBClear(keysToKeep);
await $A.IDBSet('cacheVersion', state.cacheVersion);
// userInfo
await dispatch("saveUserInfoBase", $A.isJson(userData) ? userData : cacheItems.userInfo)
const cachedUserInfo = await $A.IDBJson("userInfo");
await dispatch("saveUserInfoBase", $A.isJson(userData) ? userData : cachedUserInfo)
// readCache
await dispatch("handleReadCache")