dashboardCacheRepo.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "fwh-toolbox-server/cache"
  6. "fwh-toolbox-server/domain"
  7. "time"
  8. "go.uber.org/zap"
  9. )
  10. var (
  11. dashboardCacheCtx = context.Background()
  12. dashboardCacheKeyPrefix = "fwhtoolbox:dashboard:"
  13. )
  14. // Shutdown
  15. var (
  16. dashboardCacheShutdownDateKey = dashboardCacheKeyPrefix + "shutdownDate"
  17. dashboardCacheShutdownWeeklyKey = dashboardCacheKeyPrefix + "shutdownWeekly"
  18. dashboardCacheShutdownYearlyKey = dashboardCacheKeyPrefix + "shutdownYearly"
  19. )
  20. // Alarm
  21. var (
  22. dashboardCacheAlarmDateKey = dashboardCacheKeyPrefix + "alarmDate"
  23. dashboardCacheAlarmCodeKey = dashboardCacheKeyPrefix + "alarmCodeStatistics"
  24. dashboardCacheAlarmSiteKey = dashboardCacheKeyPrefix + "alarmSiteStatistics"
  25. )
  26. type RedisDashboardCache struct{}
  27. var DashboardCache RedisDashboardCache = RedisDashboardCache{}
  28. func (c *RedisDashboardCache) GetShutdownDate() (*string, error) {
  29. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  30. defer cancel()
  31. var date string
  32. err := cache.GetJSON(ctx, dashboardCacheShutdownDateKey, &date)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &date, nil // 命中缓存
  37. }
  38. func (c *RedisDashboardCache) GetShutdownWeekly() (*domain.ShutdownStatisticsArea[[7]uint], error) {
  39. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  40. defer cancel()
  41. var data domain.ShutdownStatisticsArea[[7]uint]
  42. err := cache.GetJSON(ctx, dashboardCacheShutdownWeeklyKey, &data)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &data, nil // 命中缓存
  47. }
  48. func (c *RedisDashboardCache) GetShutdownYearly() (*domain.ShutdownStatisticsArea[[12]uint], error) {
  49. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  50. defer cancel()
  51. var data domain.ShutdownStatisticsArea[[12]uint]
  52. err := cache.GetJSON(ctx, dashboardCacheShutdownYearlyKey, &data)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return &data, nil // 命中缓存
  57. }
  58. func (c *RedisDashboardCache) InsertShutdown(data domain.ShutdownStatisticsDashboard) error {
  59. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  60. defer cancel()
  61. var isErr bool = false
  62. if err := cache.SetJSON(ctx, dashboardCacheShutdownDateKey, data.Date, time.Hour*24); err != nil {
  63. zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error()))
  64. isErr = true
  65. }
  66. if err := cache.SetJSON(ctx, dashboardCacheShutdownWeeklyKey, data.Weekly, time.Hour*24); err != nil {
  67. zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error()))
  68. isErr = true
  69. }
  70. if err := cache.SetJSON(ctx, dashboardCacheShutdownYearlyKey, data.Yearly, time.Hour*24); err != nil {
  71. zap.L().Warn("[RedisDashboardCache] InsertShutdown failed", zap.String("err", err.Error()))
  72. isErr = true
  73. }
  74. if isErr {
  75. c.ClearShutdown()
  76. return fmt.Errorf("[RedisDashboardCache] InsertShutdown failed")
  77. }
  78. return nil
  79. }
  80. func (c *RedisDashboardCache) ClearShutdown() error {
  81. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  82. defer cancel()
  83. return cache.Delete(ctx, dashboardCacheShutdownDateKey, dashboardCacheShutdownWeeklyKey, dashboardCacheShutdownYearlyKey)
  84. }
  85. func (c *RedisDashboardCache) GetAlarmDate() (*string, error) {
  86. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  87. defer cancel()
  88. var date string
  89. err := cache.GetJSON(ctx, dashboardCacheAlarmDateKey, &date)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &date, nil // 命中缓存
  94. }
  95. func (c *RedisDashboardCache) GetAlarmCodeStatistics() (*map[string]uint, error) {
  96. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  97. defer cancel()
  98. var codeStatistics map[string]uint
  99. err := cache.GetJSON(ctx, dashboardCacheAlarmCodeKey, &codeStatistics)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &codeStatistics, nil // 命中缓存
  104. }
  105. func (c *RedisDashboardCache) GetAlarmSiteStatistics() (*uint, error) {
  106. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  107. defer cancel()
  108. var siteStatistics uint
  109. err := cache.GetJSON(ctx, dashboardCacheAlarmSiteKey, &siteStatistics)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &siteStatistics, nil // 命中缓存
  114. }
  115. func (c *RedisDashboardCache) InsertAlarm(data domain.AlarmStatisticsDashboard) error {
  116. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  117. defer cancel()
  118. var isErr bool = false
  119. if err := cache.SetJSON(ctx, dashboardCacheAlarmDateKey, data.Date, time.Hour*24); err != nil {
  120. zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error()))
  121. isErr = true
  122. }
  123. if err := cache.SetJSON(ctx, dashboardCacheAlarmCodeKey, data.CodeStatistics, time.Hour*24); err != nil {
  124. zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error()))
  125. isErr = true
  126. }
  127. if err := cache.SetJSON(ctx, dashboardCacheAlarmSiteKey, data.SiteStatistics, time.Hour*24); err != nil {
  128. zap.L().Warn("[RedisDashboardCache] InsertAlarm failed", zap.String("err", err.Error()))
  129. isErr = true
  130. }
  131. if isErr {
  132. c.ClearShutdown()
  133. return fmt.Errorf("[RedisDashboardCache] InsertAlarm failed")
  134. }
  135. return nil
  136. }
  137. func (c *RedisDashboardCache) ClearAlarm() error {
  138. ctx, cancel := context.WithTimeout(dashboardCacheCtx, 5*time.Second)
  139. defer cancel()
  140. return cache.Delete(ctx, dashboardCacheAlarmDateKey, dashboardCacheAlarmCodeKey, dashboardCacheAlarmSiteKey)
  141. }