- 消除 system_configs 写锁争抢:使用读写锁与启动预热将默认配置初始化收敛为单例,杜绝读请求开事务导致的 Lock wait timeout exceeded - 强化财务看板缓存:将 Redis 缓存有效期由 1 分钟延长为 5 分钟,接入 singleflight.Group 防并发击穿 - 修复提号列表 GORM 翻页 Bug:分离 Count 查询与列表查询构建,避免重复 Join 导致的 Error 1066 - 生产环境资源加固:docker-compose.prod.yml 中 MySQL 资源限制提升至 2.0 核 CPU、2G 内存 - 补充 systemconfig 并发测试与 pickup 分页 Count 回归测试
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const dashboardCacheTTL = 5 * time.Minute
|
|
|
|
func (r *Repository) dashboardCacheKey(query DashboardQuery) string {
|
|
return fmt.Sprintf("admin-finance:dashboard:v1:%d:%d", query.StartDate.Unix(), query.EndDate.Unix())
|
|
}
|
|
|
|
func (r *Repository) loadDashboardCache(ctx context.Context, query DashboardQuery) *DashboardDTO {
|
|
if r.redis == nil {
|
|
return nil
|
|
}
|
|
raw, err := r.redis.Get(ctx, r.dashboardCacheKey(query)).Bytes()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var value DashboardDTO
|
|
if err := json.Unmarshal(raw, &value); err != nil {
|
|
return nil
|
|
}
|
|
return &value
|
|
}
|
|
|
|
func (r *Repository) storeDashboardCache(ctx context.Context, query DashboardQuery, value *DashboardDTO) {
|
|
if r.redis == nil || value == nil {
|
|
return
|
|
}
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return
|
|
}
|
|
// 缓存不可用时静默降级到实时统计,不能影响财务页面可用性。
|
|
_ = r.redis.Set(ctx, r.dashboardCacheKey(query), raw, dashboardCacheTTL).Err()
|
|
}
|