close
Skip to content

feat(setting): 支持自定义缓存大小上限,更改 cacheLimit 单位为字节#642

Closed
MoYingJi wants to merge 1 commit intoimsyy:devfrom
MoYingJi:feat-cache-size
Closed

feat(setting): 支持自定义缓存大小上限,更改 cacheLimit 单位为字节#642
MoYingJi wants to merge 1 commit intoimsyy:devfrom
MoYingJi:feat-cache-size

Conversation

@MoYingJi
Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @MoYingJi, 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!

此拉取请求主要改进了应用程序的缓存大小限制功能。通过将内部缓存限制单位统一为字节,并为用户提供更灵活的自定义设置选项(包括数值输入和单位选择),极大地增强了用户体验和系统稳定性。此外,还引入了2GB的最小缓存限制,以防止意外设置过小的缓存导致问题。

Highlights

  • 缓存大小单位统一为字节: 内部存储和处理缓存大小时,将单位从GB统一为字节,提高了精度和一致性。
  • 支持自定义缓存大小上限: 用户现在可以通过输入数值和选择单位(MB/GB)来设置缓存大小上限,取代了之前固定的选项。
  • 引入缓存大小最小限制: 强制设置了2GB的缓存大小下限,以确保缓存功能正常运行。
  • UI界面优化: 缓存大小设置界面进行了重新设计,提供了更灵活的数值输入框和单位选择器。
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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 实现了自定义缓存大小上限的功能,并将单位从 GB 改为字节,这是一个很好的功能增强。代码整体实现不错,但在几个地方存在问题:

  1. 缺少对 electron-store 中现有 cacheLimit 配置的迁移逻辑,这可能导致现有用户的缓存设置失效。
  2. 在设置页面加载时,缓存上限的恢复逻辑不正确,会用默认值覆盖用户的既有设置。
  3. 代码中存在魔术数字,不利于维护。

我已经在代码中提出了具体的修改建议,请查阅。

amllDbServer: defaultAMLLDbServer,
cachePath: join(app.getPath("userData"), "DataCache"),
cacheLimit: 10, // 默认 10GB
cacheLimit: 10 * (1024 ** 3), // 默认 10GB
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.

critical

此更改将 cacheLimit 的单位从 GB 更改为字节,但缺少对现有用户配置的数据迁移逻辑。对于现有用户,其 electron-store 中存储的 cacheLimit 值(例如 10 代表 10GB)将被错误地解释为字节(10 字节),这会破坏缓存功能。需要在应用启动时添加一次性迁移逻辑,将旧的 GB 值转换为字节。

建议在 useStore 函数中,初始化 Store 实例后添加如下迁移代码:

const store = new Store<StoreType>(...);

const cacheLimit = store.get('cacheLimit');
// 假设旧的 GB 值是小于 1000 的小整数
if (cacheLimit > 0 && cacheLimit < 1000) {
  store.set('cacheLimit', cacheLimit * (1024 ** 3));
}

return store;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

仍在开发中,加入此功能到此 PR 期间没有发布 Release

Comment on lines +457 to +458
// 恢复数值
await updateCacheLimit();
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

onMounted 钩子中,通过调用 updateCacheLimit() 来恢复缓存限制的数值,这是不正确的。updateCacheLimit 会将 Pinia store (UI state) 中的值写入 electron-store,这会导致覆盖用户在 electron-store 中已有的设置。正确的逻辑应该是从 electron-store 读取值,然后更新 Pinia store 来初始化 UI。

    // 恢复数值
    const limitInBytes = await window.api.store.get("cacheLimit");
    if (typeof limitInBytes === "number") {
      if (limitInBytes === 0) {
        settingStore.cacheLimitMultiplier = 0;
        settingStore.cacheLimitValue = 10; // 或其他合适的默认值
      } else if (limitInBytes >= 1024 ** 3 && limitInBytes % (1024 ** 3) === 0) {
        settingStore.cacheLimitMultiplier = 1024 ** 3; // GB
        settingStore.cacheLimitValue = limitInBytes / (1024 ** 3);
      } else {
        settingStore.cacheLimitMultiplier = 1024 ** 2; // MB
        settingStore.cacheLimitValue = Math.round(limitInBytes / (1024 ** 2));
      }
    }
    // 确保数值符合最小限制
    await updateCacheLimit();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

但这样就丢失掉了单位的信息诶
比如我设置了 10240 MB 的大小,下次打开就自动变成 10 GB 了,单位信息丢掉了

await this.cacheService.cleanOldCache(
"music",
currentSize - limitSizeBytes + 100 * 1024 * 1024,
currentSize - limitSizeBytes + (100 * 1024 * 1024),
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

这里的 100 * 1024 * 1024 (100MB) 是一个“魔术数字”。为了提高代码的可读性和可维护性,建议在函数开头将其定义为一个具名常量,例如 const MIN_FREE_SPACE_BYTES = 100 * 1024 * 1024;,然后在这里使用该常量。

Suggested change
currentSize - limitSizeBytes + (100 * 1024 * 1024),
currentSize - limitSizeBytes + MIN_FREE_SPACE_BYTES,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

我啥也没改啊 我就加了个括号而已哈基米大人放过我吧

@imsyy
Copy link
Copy Markdown
Owner

imsyy commented Dec 20, 2025

@MoYingJi
缓存数值可以只保留不限制和自定义 GB 单位( 可以小数 ),符合大众认知,并且不用互相转换
缓存数值不能放在 pinia 中,否则会造成重置配置后两边对不上,只能从 electron-store 中读和写

@MoYingJi
Copy link
Copy Markdown
Collaborator Author

MoYingJi commented Dec 20, 2025

那感觉就和原来没啥区别了哇 在这基础上改还不如重新水了.jpg(

#644

@MoYingJi MoYingJi closed this Dec 20, 2025
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.

2 participants