package redis import ( "context" "fmt" "fwh-toolbox-server/cache" "fwh-toolbox-server/domain" "time" "go.uber.org/zap" ) var ( dashboardCacheCtx = context.Background() dashboardCacheKeyPrefix = "fwhtoolbox:dashboard:" ) // Shutdown var ( dashboardCacheShutdownDateKey = dashboardCacheKeyPrefix + "shutdownDate" dashboardCacheShutdownWeeklyKey = dashboardCacheKeyPrefix + "shutdownWeekly" dashboardCacheShutdownYearlyKey = dashboardCacheKeyPrefix + "shutdownYearly" ) // Alarm var ( dashboardCacheAlarmDateKey = dashboardCacheKeyPrefix + "alarmDate" dashboardCacheAlarmCodeKey = dashboardCacheKeyPrefix + "alarmCodeStatistics" dashboardCacheAlarmSiteKey = dashboardCacheKeyPrefix + "alarmSiteStatistics" ) type RedisDashboardCache struct{} var DashboardCache RedisDashboardCache = RedisDashboardCache{} func (c *RedisDashboardCache) GetShutdownDate() (*string, error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var date string err := cache.GetJSON(ctx, dashboardCacheShutdownDateKey, &date) if err != nil { return nil, err } return &date, nil // 命中缓存 } func (c *RedisDashboardCache) GetShutdownWeekly() (*domain.ShutdownStatisticsArea[[7]uint], error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var data domain.ShutdownStatisticsArea[[7]uint] err := cache.GetJSON(ctx, dashboardCacheShutdownWeeklyKey, &data) if err != nil { return nil, err } return &data, nil // 命中缓存 } func (c *RedisDashboardCache) GetShutdownYearly() (*domain.ShutdownStatisticsArea[[12]uint], error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var data domain.ShutdownStatisticsArea[[12]uint] err := cache.GetJSON(ctx, dashboardCacheShutdownYearlyKey, &data) if err != nil { return nil, err } return &data, nil // 命中缓存 } func (c *RedisDashboardCache) InsertShutdown(data domain.ShutdownStatisticsDashboard) error { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var isErr bool = false if err := cache.SetJSON(ctx, dashboardCacheShutdownDateKey, data.Date, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error())) isErr = true } if err := cache.SetJSON(ctx, dashboardCacheShutdownWeeklyKey, data.Weekly, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error())) isErr = true } if err := cache.SetJSON(ctx, dashboardCacheShutdownYearlyKey, data.Yearly, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error())) isErr = true } if isErr { c.ClearShutdown() return fmt.Errorf("[RedisDashboardCache] InsertShutdown failed") } return nil } func (c *RedisDashboardCache) ClearShutdown() error { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() return cache.Delete(ctx, dashboardCacheShutdownDateKey, dashboardCacheShutdownWeeklyKey, dashboardCacheShutdownYearlyKey) } func (c *RedisDashboardCache) GetAlarmDate() (*string, error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var date string err := cache.GetJSON(ctx, dashboardCacheAlarmDateKey, &date) if err != nil { return nil, err } return &date, nil // 命中缓存 } func (c *RedisDashboardCache) GetAlarmCodeStatistics() (*map[string]uint, error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var codeStatistics map[string]uint err := cache.GetJSON(ctx, dashboardCacheAlarmCodeKey, &codeStatistics) if err != nil { return nil, err } return &codeStatistics, nil // 命中缓存 } func (c *RedisDashboardCache) GetAlarmSiteStatistics() (*uint, error) { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var siteStatistics uint err := cache.GetJSON(ctx, dashboardCacheAlarmSiteKey, &siteStatistics) if err != nil { return nil, err } return &siteStatistics, nil // 命中缓存 } func (c *RedisDashboardCache) InsertAlarm(data domain.AlarmStatisticsDashboard) error { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() var isErr bool = false if err := cache.SetJSON(ctx, dashboardCacheAlarmDateKey, data.Date, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error())) isErr = true } if err := cache.SetJSON(ctx, dashboardCacheAlarmCodeKey, data.CodeStatistics, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error())) isErr = true } if err := cache.SetJSON(ctx, dashboardCacheAlarmSiteKey, data.SiteStatistics, time.Hour*24); err != nil { zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error())) isErr = true } if isErr { c.ClearShutdown() return fmt.Errorf("[RedisDashboardCache] InsertAlarm failed") } return nil } func (c *RedisDashboardCache) ClearAlarm() error { ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second) defer cancel() return cache.Delete(ctx, dashboardCacheAlarmDateKey, dashboardCacheAlarmCodeKey, dashboardCacheAlarmSiteKey) }