feat: Implement caching for settings retrieval and update logic#11810
feat: Implement caching for settings retrieval and update logic#11810HynoR wants to merge 3 commits into1Panel-dev:dev-v2from
Conversation
- 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.
|
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. DetailsInstructions 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. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
- 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.
|
先 draft 了 2.0.18 大改动已经很多了, 暂时不合这个,等合进 2.0.19。 |
- 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.
2.0.18 改动已经很多了, 暂时不合这个,等合进 2.0.19
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:#000000Please indicate you've done the following: