package adminfinance import ( "context" "encoding/json" "fmt" "time" "hfb_sys/backend/internal/tenancy" ) const dashboardCacheTTL = 5 * time.Minute func (r *Repository) dashboardCacheKey(ctx context.Context, query DashboardQuery) string { return fmt.Sprintf("admin-finance:dashboard:v3:%d:%d:%d", tenancy.IDFromContext(ctx), 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(ctx, 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(ctx, query), raw, dashboardCacheTTL).Err() }