Skip to content

refactor(music-cache): 模糊匹配时按音质优先级自动选择最佳缓存#1009

Merged
MoYingJi merged 27 commits into
SPlayer-Dev:devfrom
laoshuikaixue:feat/electron-cache-quality-md5
Apr 6, 2026
Merged

refactor(music-cache): 模糊匹配时按音质优先级自动选择最佳缓存#1009
MoYingJi merged 27 commits into
SPlayer-Dev:devfrom
laoshuikaixue:feat/electron-cache-quality-md5

Conversation

@laoshuikaixue
Copy link
Copy Markdown
Contributor

@laoshuikaixue laoshuikaixue commented Mar 20, 2026

  • 添加音质优先级映射表,支持多种音质等级排序
  • 实现 pickCandidates 方法按音质优先级和修改时间选择候选缓存
  • 重构 hasCache 方法支持按音质优先级自动选择最佳缓存

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在全面提升音乐缓存服务的智能性和可靠性。通过引入音质优先级管理和独立的缓存元数据文件,系统现在能够更精确地管理和选择缓存的音乐文件,确保用户始终获得最佳音质体验。同时,MD5验证机制得到了显著增强,利用元数据缓存避免了重复计算,从而提高了缓存验证的效率和准确性。

Highlights

  • 音质优先级管理: 添加了音质优先级映射表,支持多种音质等级排序,使得系统能够根据预设权重选择最佳音质缓存。
  • 缓存元数据管理: 实现了缓存元数据文件管理,用于存储MD5和文件大小信息,并新增了 getMetaPathreadMetawriteMeta 等元数据操作方法。
  • 智能缓存选择: 新增了 pickCandidates 方法,能够根据音质优先级和修改时间智能选择候选缓存。
  • MD5验证机制优化: 重构了 hasCache 方法,使其支持按音质优先级自动选择最佳缓存,并改进了MD5验证逻辑,利用元数据缓存减少重复计算,提高验证效率。
  • 下载后元数据写入: 优化了下载完成后的元数据写入流程,确保缓存文件的MD5和大小信息及时更新并存储。
  • 环境变量配置: 新增了 .env 文件,用于配置Web和API端口以及API地址。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 PR 极大地优化了音乐缓存服务,引入了音质优先级和基于元数据的 MD5 校验机制,是非常出色的改进。代码结构清晰,新功能考虑周全。我提出了一些建议,主要集中在错误处理和性能优化方面,希望能让代码更加健壮和高效。

Comment thread electron/main/services/MusicCacheService.ts Outdated
Comment thread electron/main/services/MusicCacheService.ts Outdated
Comment thread electron/main/services/MusicCacheService.ts Outdated
@MoYingJi
Copy link
Copy Markdown
Collaborator

你是不是切错分支了(

@laoshuikaixue
Copy link
Copy Markdown
Contributor Author

laoshuikaixue commented Mar 22, 2026 via email

@MoYingJi MoYingJi marked this pull request as draft March 22, 2026 15:47
Comment thread electron/main/services/MusicCacheService.ts Outdated
- 添加了精确缓存检查和获取候选缓存的错误捕获及警告日志
- 将元数据写入改为异步执行避免阻塞主流程
- 添加了元数据写入失败的错误处理和警告日志
- 保持了文件重命名和缓存大小记录功能的正常执行
@laoshuikaixue laoshuikaixue marked this pull request as ready for review April 5, 2026 11:05
Comment thread electron/main/services/MusicCacheService.ts Outdated
Co-authored-by: MoYingJi <moyingjiaw@outlook.com>
Copy link
Copy Markdown
Collaborator

@MoYingJi MoYingJi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不太认同使用 .meta.json 文件存储元数据

我觉得校验哈希并不占太多时间,基本可以忽略不计

# 大小最大的文件
> du -h ./* | sort --numeric-sort | tail -1
92M     ./2630874953_Hi-Res.sc

# 测量时间
> time md5sum 2630874953_Hi-Res.sc
cd7fb8a62ee113b20dc6d6d0d955684a  2630874953_Hi-Res.sc

real    0m0.100s
user    0m0.089s
sys     0m0.011s

即使你认为计算 md5sum 的时间会拖慢缓存检索速度,也可以使用项目已有的数据库存储这些信息,而不是新建一个文件(我相信当时把歌词缓存从 json 文件换成数据库也是有理由的)。现在直接新建了一个 .meta.json 文件,还是和 .sc 混在一起放

- 移除文件系统元数据文件读写,改用缓存服务存储
- 删除不再使用的 getMetaPath 方法
- 更新 readMeta 方法以从缓存中读取元数据
- 修改 writeMeta 方法将元数据写入缓存服务
- 调整 removeCacheWithMeta 方法使用缓存服务删除元数据
- 简化元数据键名生成逻辑
@laoshuikaixue
Copy link
Copy Markdown
Contributor Author

@gemini-code-assist review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the MusicCacheService by implementing a quality priority system and a more robust cache selection mechanism. When checking for a cache, the service now sorts candidates by audio quality and modification time. The review feedback suggests normalizing quality strings to be case-insensitive, implementing the missing metadata management for MD5 calculation mentioned in the PR description, and optimizing the file listing process to prevent performance degradation.

Comment thread electron/main/services/MusicCacheService.ts
Comment thread electron/main/services/MusicCacheService.ts
return filePath;
}
try {
const fileMD5 = await this.calculateMD5(filePath);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

PR 描述中提到实现了元数据管理以存储 MD5 并减少重复计算,包括 getMetaPathreadMetawriteMeta 等新方法。然而,当前代码中并未发现这些方法,且 hasCache 在提供 expectedMD5 时仍然通过读取整个文件来计算 MD5。请确保包含元数据缓存逻辑,以避免重复哈希带来的性能开销。

private async pickCandidates(
id: number | string,
): Promise<Array<{ filePath: string; quality: string }>> {
const items = await this.cacheService.list("music");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

pickCandidates 中调用 this.cacheService.list("music") 会触发对音乐缓存目录的完整扫描,并对每个文件执行 stat 操作。随着缓存文件数量的增加,这可能会成为显著的性能瓶颈。建议考虑为缓存的音乐文件实现索引(例如在数据库中),以便通过 ID 快速查找,而无需扫描整个文件系统。

- 将音质优先级映射中的键值从大写改为小写
- 修改getQualityWeight方法以支持大小写不敏感的音质匹配
- 确保音质比较时统一转换为小写进行匹配
@laoshuikaixue laoshuikaixue requested a review from MoYingJi April 5, 2026 12:19
Comment thread electron/main/services/MusicCacheService.ts Outdated
- 删除了 private removeCacheFile 方法
- 将文件删除逻辑直接内联到调用处
- 保持相同的错误处理行为
@laoshuikaixue laoshuikaixue requested a review from MoYingJi April 5, 2026 12:45
Copy link
Copy Markdown
Collaborator

@MoYingJi MoYingJi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该差不多了,可以补点注释上去

@MoYingJi MoYingJi changed the title feat(electron): 优化音乐缓存服务的音质优先级和MD5验证机制 refactor(music-cache): 模糊匹配时按音质优先级自动选择最佳缓存 Apr 5, 2026
@MoYingJi
Copy link
Copy Markdown
Collaborator

MoYingJi commented Apr 5, 2026

把标题和内容改了下,更符合当前 PR 的更改

Comment thread electron/main/services/MusicCacheService.ts Outdated
- 移除注释中的枚举值兼容性说明
- 统一音质键名的字符串格式
- 删除冗余的音质选项如lossless、flac、exhigh等
- 保留核心音质等级配置
- 优化对象结构提高可读性
Comment on lines +16 to +28
/** 音质优先级 */
private readonly qualityPriority: Record<string, number> = {
"master": 100,
"dolby": 95,
"spatial": 90,
"surround": 85,
"hi-res": 80,
"sq": 70,
"hq": 60,
"mq": 50,
"lq": 40,
"standard": 40,
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在思考 masterdolbyspatialsurround 这四个 AI 音质是否要给到更高的优先级,尤其是 dolby(杜比全景声)和 surround(沉浸环绕声)和其它的普通音质都不在一组内了

@MoYingJi MoYingJi merged commit bea6f8f into SPlayer-Dev:dev Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants