LingxinMeng 9 months ago
parent
commit
e22f0e8767
1 changed files with 29 additions and 0 deletions
  1. 29 0
      common/service/cache_monitor_queue.go

+ 29 - 0
common/service/cache_monitor_queue.go

@@ -0,0 +1,29 @@
+package service
+
+import (
+	"cicv-data-closedloop/common/util"
+	"sync"
+)
+
+var (
+	MonitorQueue = make([]MonitorInfo, 0, QueueLength)
+	QueueLength  = 120
+	mu           sync.Mutex // 用于保护MonitorQueue的互斥锁
+)
+
+type MonitorInfo struct {
+	Time string
+}
+
+// CacheMonitorQueue 向MonitorQueue中添加一个新的MonitorInfo实例,如果队列已满,则替换最旧的元素
+func CacheMonitorQueue() {
+	mu.Lock()
+	defer mu.Unlock()
+	// 创建新的MonitorInfo实例,Time字段为当前时间的字符串表示
+	// 如果队列长度已经达到上限,移除最旧的元素
+	if len(MonitorQueue) >= QueueLength {
+		MonitorQueue = MonitorQueue[1:] // 移除第一个元素
+	}
+	// 将新的MonitorInfo实例添加到队列的末尾
+	MonitorQueue = append(MonitorQueue, MonitorInfo{Time: util.GetNowTimeCustom()})
+}