Skip to content

Commit a34c64b

Browse files
committed
🎈 perf: 优化日志存储位置及缓存过期优化
1 parent 88db937 commit a34c64b

6 files changed

Lines changed: 57 additions & 11 deletions

File tree

electron/main/logger/index.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// 日志输出
2-
import { existsSync, mkdirSync } from "fs";
2+
import { existsSync, mkdirSync, readdirSync, statSync, unlinkSync } from "fs";
33
import { join } from "path";
44
import { app } from "electron";
5+
import { isDev } from "../utils/config";
56
import log from "electron-log";
67

78
// 日志文件路径
8-
const logDir = join(app.getPath("logs"));
9+
const logDir = isDev ? join(app.getPath("logs"), "dev") : join(app.getPath("logs"));
910

1011
// 是否存在日志目录
1112
if (!existsSync(logDir)) mkdirSync(logDir);
@@ -20,8 +21,29 @@ log.transports.file.level = "info"; // 仅记录 info 及以上级别
2021
log.transports.file.resolvePathFn = (): string => logFilePath; // 日志文件路径
2122
log.transports.file.maxSize = 2 * 1024 * 1024; // 文件最大 2MB
2223

23-
// 日志格式化
24-
// log.transports.file.format = "[{y}-{m}-{d} {h}:{i}:{s}] [{level}] [{scope}] {text}";
24+
// 自动清理
25+
const autoCleanLog = (daysToKeep: number = 30) => {
26+
try {
27+
const files = readdirSync(logDir);
28+
const now = Date.now();
29+
const msToKeep = daysToKeep * 24 * 60 * 60 * 1000;
30+
31+
files.forEach((file) => {
32+
const filePath = join(logDir, file);
33+
const stats = statSync(filePath);
34+
35+
// 如果文件修改时间早于 (当前时间 - 需要保留的天数),则删除
36+
if (now - stats.mtimeMs > msToKeep) {
37+
unlinkSync(filePath);
38+
console.log(`已清理旧日志: ${file}`);
39+
}
40+
});
41+
} catch (err) {
42+
console.error("清理日志失败:", err);
43+
}
44+
};
45+
46+
autoCleanLog();
2547

2648
// 绑定默认事件
2749
const defaultLog = log.scope("default");

electron/main/services/CacheService.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join, resolve, dirname } from "path";
22
import { existsSync } from "fs";
3-
import { readdir, readFile, rm, stat, writeFile, mkdir } from "fs/promises";
3+
import { readdir, readFile, rm, stat, writeFile, mkdir, utimes } from "fs/promises";
44
import { useStore } from "../store";
55
import { cacheLog } from "../logger";
66

@@ -21,6 +21,8 @@ export interface CacheListItem {
2121
key: string;
2222
/** 文件大小(字节) */
2323
size: number;
24+
/** 最后访问时间(毫秒时间戳) */
25+
atime: number;
2426
/** 最后修改时间(毫秒时间戳) */
2527
mtime: number;
2628
}
@@ -231,6 +233,15 @@ export class CacheService {
231233
await this.init();
232234
const { target } = this.resolveSafePath(type, key);
233235
if (!existsSync(target)) return null;
236+
237+
// 手动更新 atime (最后访问时间),实现 LRU 逻辑
238+
try {
239+
const now = new Date();
240+
await utimes(target, now, now);
241+
} catch (e) {
242+
// 忽略 utimes 失败
243+
}
244+
234245
return await readFile(target);
235246
}
236247

@@ -282,6 +293,7 @@ export class CacheService {
282293
items.push({
283294
key: file.name,
284295
size: info.size,
296+
atime: info.atimeMs,
285297
mtime: info.mtimeMs,
286298
});
287299
}
@@ -290,7 +302,7 @@ export class CacheService {
290302
}
291303

292304
/**
293-
* 清理旧缓存(LRU:删除最早修改的文件)
305+
* 清理旧缓存
294306
* @param type 缓存类型
295307
* @param targetFreeSize 需要腾出的空间大小
296308
*/
@@ -299,8 +311,8 @@ export class CacheService {
299311
let freedSize = 0;
300312
const items = await this.list(type);
301313

302-
// 按 mtime 升序排序 (旧的在前)
303-
items.sort((a, b) => a.mtime - b.mtime);
314+
// 按 atime 升序排序 (最久未访问的在前)
315+
items.sort((a, b) => a.atime - b.atime);
304316

305317
for (const item of items) {
306318
if (freedSize >= targetFreeSize) break;

src/components/Global/SvgIcon.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ onMounted(() => {
7979
:deep(svg) {
8080
width: 1em;
8181
height: 1em;
82-
fill: currentColor; // 允许通过 n-icon 的 color 属性控制颜色
82+
fill: currentColor;
8383
}
8484
}
8585
}

src/core/player/SongManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { openUserLogin } from "@/utils/modal";
1414
export enum SongUnlockServer {
1515
NETEASE = "netease",
1616
BODIAN = "bodian",
17-
// KUWO = "kuwo",
17+
KUWO = "kuwo",
1818
GEQUBAO = "gequbao",
1919
}
2020

src/stores/migrations/settingMigrations.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { SongUnlockServer } from "@/core/player/SongManager";
12
import type { SettingState } from "../setting";
23
import { defaultAMLLDbServer } from "@/utils/meta";
34

45
/**
56
* 当前设置 Schema 版本号
67
*/
7-
export const CURRENT_SETTING_SCHEMA_VERSION = 3;
8+
export const CURRENT_SETTING_SCHEMA_VERSION = 4;
89

910
/**
1011
* 迁移函数类型
@@ -25,5 +26,15 @@ export const settingMigrations: Record<number, MigrationFunction> = {
2526
amllDbServer: defaultAMLLDbServer,
2627
};
2728
},
29+
4: () => {
30+
return {
31+
songUnlockServer: [
32+
{ key: SongUnlockServer.BODIAN, enabled: true },
33+
{ key: SongUnlockServer.GEQUBAO, enabled: true },
34+
{ key: SongUnlockServer.NETEASE, enabled: true },
35+
{ key: SongUnlockServer.KUWO, enabled: false },
36+
],
37+
};
38+
},
2839
};
2940

src/stores/setting.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ export const useSettingStore = defineStore("setting", {
304304
{ key: SongUnlockServer.BODIAN, enabled: true },
305305
{ key: SongUnlockServer.GEQUBAO, enabled: true },
306306
{ key: SongUnlockServer.NETEASE, enabled: true },
307+
{ key: SongUnlockServer.KUWO, enabled: false },
307308
],
308309
countDownShow: true,
309310
barLyricShow: true,

0 commit comments

Comments
 (0)