Skip to content

Commit 9d123ea

Browse files
committed
🐞 fix: 修复无法清空缓存
1 parent ac1f9af commit 9d123ea

5 files changed

Lines changed: 58 additions & 11 deletions

File tree

electron/main/database/CacheDB.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export class CacheDB {
8383
this.db.prepare("DELETE FROM kv_cache WHERE type = ?").run(type);
8484
}
8585

86+
/** 清空所有缓存并回收空间 */
87+
clearAll() {
88+
this.db.exec("DELETE FROM kv_cache");
89+
this.db.exec("VACUUM");
90+
}
91+
8692
/** 列出某类缓存 */
8793
list(type: string): Omit<CacheEntry, "data">[] {
8894
return this.db

electron/main/ipc/ipc-cache.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ const initCacheIpc = (): void => {
9999
},
100100
);
101101

102+
// 清空所有缓存
103+
ipcMain.handle("cache-clear-all", (): Promise<CacheIpcResult<null>> => {
104+
return withErrorCatch(async () => {
105+
await cacheService.clearAll();
106+
return null;
107+
});
108+
});
109+
102110
// 获取所有缓存类型的总大小
103111
ipcMain.handle("cache-size", (): Promise<CacheIpcResult<number>> => {
104112
return withErrorCatch(async () => {

electron/main/services/CacheService.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,39 @@ export class CacheService {
388388
}
389389
}
390390

391+
/**
392+
* 清空所有缓存
393+
* 软清空:保留 DB 文件,仅清空数据和媒体文件
394+
*/
395+
public async clearAll(): Promise<void> {
396+
await this.init();
397+
398+
// 清空 DB 数据并回收空间
399+
try {
400+
this.db?.clearAll();
401+
} catch (e) {
402+
cacheLog.error("Failed to clear database:", e);
403+
}
404+
405+
// 删除文件缓存目录内容
406+
const fileTypes: CacheResourceType[] = ["music", "local-data"];
407+
for (const type of fileTypes) {
408+
const basePath = this.getCacheBasePath();
409+
const dir = join(basePath, this.CACHE_SUB_DIR[type]);
410+
411+
try {
412+
if (existsSync(dir)) {
413+
// 删除目录并重建
414+
await rm(dir, { recursive: true, force: true });
415+
await mkdir(dir, { recursive: true });
416+
}
417+
} catch (e) {
418+
cacheLog.error(`Failed to clear directory ${type}:`, e);
419+
}
420+
this.fileSizes[type] = 0;
421+
}
422+
}
423+
391424
/**
392425
* 列出缓存文件
393426
*/

src/components/Setting/LocalSetting.vue

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
import { useSettingStore, useStatusStore } from "@/stores";
340340
import { changeLocalLyricPath, changeLocalMusicPath, formatFileSize } from "@/utils/helper";
341341
import { songLevelData, getSongLevelsData } from "@/utils/meta";
342-
import { useCacheManager, type CacheResourceType } from "@/core/resource/CacheManager";
342+
import { useCacheManager } from "@/core/resource/CacheManager";
343343
import { pick } from "lodash-es";
344344
345345
const statusStore = useStatusStore();
@@ -435,17 +435,10 @@ const loadCacheSize = async () => {
435435
436436
// 清空所有缓存目录
437437
const clearCache = async () => {
438-
const types: CacheResourceType[] = ["music", "lyrics", "local-data", "list-data"];
439-
let hasError = false;
440-
for (const type of types) {
441-
const res = await cacheManager.clear(type);
442-
if (!res.success) {
443-
hasError = true;
444-
}
445-
}
438+
const res = await cacheManager.clearAll();
446439
await loadCacheSize();
447-
if (hasError) {
448-
window.$message.error("部分缓存清理失败");
440+
if (!res.success) {
441+
window.$message.error("缓存清理失败: " + (res.message || "未知错误"));
449442
} else {
450443
window.$message.success("缓存已清空");
451444
}

src/core/resource/CacheManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ class CacheManager {
105105
return this.invoke("cache-clear", type);
106106
}
107107

108+
/**
109+
* 清空所有缓存
110+
*/
111+
clearAll(): Promise<CacheResult<null>> {
112+
return this.invoke("cache-clear-all");
113+
}
114+
108115
/**
109116
* 获取所有缓存类型的总大小(字节)
110117
*/

0 commit comments

Comments
 (0)