Skip to content

feat: Implement caching for settings retrieval and update logic#11810

Open
HynoR wants to merge 3 commits into1Panel-dev:dev-v2from
HynoR:feat/cacheforcore
Open

feat: Implement caching for settings retrieval and update logic#11810
HynoR wants to merge 3 commits into1Panel-dev:dev-v2from
HynoR:feat/cacheforcore

Conversation

@HynoR
Copy link
Contributor

@HynoR HynoR commented Feb 6, 2026

2.0.18 改动已经很多了, 暂时不合这个,等合进 2.0.19

  • Introduced a caching mechanism for settings using go-cache to improve performance.
  • Updated the Create, Update, and UpdateOrCreate methods to cache values after database operations.
  • Modified the Get and GetValueByKey methods to utilize the cache for faster access.
  • Adjusted middleware to use the new GetValueByKey method for retrieving settings, enhancing code consistency.

What this PR does / why we need it?

中间件需要多次读取 sqlite 里的数据,在 io 差的服务器可能会造成一定负担

Summary of your change

给中间件所需参数缓存进 go-cache内存(五分钟缓存,更新会刷新缓存),减少数据库 io 调用

流程图 by AI

graph TB
    Start([HTTP 请求开始]) --> GlobalLoading[GlobalLoading 中间件]
    
    GlobalLoading --> |读取缓存| SystemStatus["🔑 SystemStatus<br/>(系统状态)"]
    SystemStatus --> |Free| SetPasswordPublicKey[SetPasswordPublicKey 中间件]
    SystemStatus --> |非 Free| Error407[返回 407 错误]
    
    SetPasswordPublicKey --> |读取缓存| PasswordPublicKey["🔑 PASSWORD_PUBLIC_KEY<br/>(RSA 公钥)"]
    PasswordPublicKey --> BindDomain[BindDomain 中间件]
    
    BindDomain --> |本地请求| WhiteAllow[WhiteAllow 中间件]
    BindDomain --> |读取缓存| BindDomainKey["🔑 BindDomain<br/>(绑定域名)"]
    BindDomainKey --> |域名匹配| WhiteAllow
    BindDomainKey --> |域名不匹配| LoadErrCode1[LoadErrCode]
    LoadErrCode1 --> |读取缓存| NoAuthSetting1["🔑 NoAuthSetting<br/>(未授权返回码)"]
    NoAuthSetting1 --> ErrorDomain[返回域名错误]
    
    WhiteAllow --> |私有 IP| SessionAuth[SessionAuth 中间件]
    WhiteAllow --> |读取缓存| AllowIPs["🔑 AllowIPs<br/>(IP 白名单)"]
    AllowIPs --> |IP 匹配| SessionAuth
    AllowIPs --> |IP 不匹配| LoadErrCode2[LoadErrCode]
    LoadErrCode2 --> |读取缓存| NoAuthSetting2["🔑 NoAuthSetting<br/>(未授权返回码)"]
    NoAuthSetting2 --> ErrorIP[返回 IP 限制错误]
    
    SessionAuth --> |auth 路径| PasswordExpired[PasswordExpired 中间件]
    SessionAuth --> |读取缓存| SessionTimeout1["🔑 SessionTimeout<br/>(会话超时时间)"]
    SessionAuth --> |读取缓存| SSL1["🔑 SSL<br/>(SSL 状态)"]
    SessionTimeout1 --> PasswordExpired
    SSL1 --> PasswordExpired
    
    PasswordExpired --> |auth 路径| Proxy[Proxy 路由处理]
    PasswordExpired --> |读取缓存| ExpirationDays["🔑 ExpirationDays<br/>(密码过期天数)"]
    PasswordExpired --> |读取缓存| ExpirationTime["🔑 ExpirationTime<br/>(密码过期时间)"]
    ExpirationDays --> |0 天| Proxy
    ExpirationDays --> |非 0 天| CheckTime{检查过期时间}
    ExpirationTime --> CheckTime
    CheckTime --> |未过期| Proxy
    CheckTime --> |已过期| Error313[返回 313 密码过期]
    
    Proxy --> |core 路径| Handler[业务处理器]
    Proxy --> |非本地 agent 路径| ProxyCheckSession[checkSession 验证]
    ProxyCheckSession --> |读取缓存| SessionTimeout2["🔑 SessionTimeout<br/>(会话超时时间)"]
    ProxyCheckSession --> |读取缓存| SSL2["🔑 SSL<br/>(SSL 状态)"]
    SessionTimeout2 --> ProxyAgent[转发到 Agent]
    SSL2 --> ProxyAgent
    
    Handler --> End([请求处理完成])
    ProxyAgent --> End
    
    style SystemStatus fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style PasswordPublicKey fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style BindDomainKey fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style NoAuthSetting1 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style AllowIPs fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style NoAuthSetting2 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style SessionTimeout1 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style SSL1 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style ExpirationDays fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style ExpirationTime fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style SessionTimeout2 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    style SSL2 fill:#e1f5ff,stroke:#0066cc,stroke-width:2px,color:#000000
    
    style GlobalLoading fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style SetPasswordPublicKey fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style BindDomain fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style WhiteAllow fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style SessionAuth fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style PasswordExpired fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
    style Proxy fill:#fff4e6,stroke:#ff9800,stroke-width:2px,color:#000000
Loading

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

- Introduced a caching mechanism for settings using go-cache to improve performance.
- Updated the Create, Update, and UpdateOrCreate methods to cache values after database operations.
- Modified the Get and GetValueByKey methods to utilize the cache for faster access.
- Adjusted middleware to use the new GetValueByKey method for retrieving settings, enhancing code consistency.
@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 6, 2026

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 6, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zhengkunwang223 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

- Introduced a function to determine cache TTL based on setting keys, allowing critical settings to have shorter cache durations.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to utilize the new TTL management, improving performance and consistency in settings retrieval.
@HynoR HynoR marked this pull request as draft February 6, 2026 07:43
@HynoR
Copy link
Contributor Author

HynoR commented Feb 6, 2026

先 draft 了 2.0.18 大改动已经很多了, 暂时不合这个,等合进 2.0.19。

@HynoR HynoR closed this Feb 6, 2026
- Removed the dynamic TTL function and standardized the cache TTL to a fixed duration for all settings.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to use the new fixed TTL, enhancing code clarity and maintainability.
- Added a new function to restart the core service after resetting settings, improving the reset command's functionality.
@HynoR HynoR reopened this Feb 7, 2026
@HynoR HynoR marked this pull request as ready for review February 7, 2026 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant