Przeglądaj źródła

Merge pull request #630 from xqx333/main

修复自动禁用会对数据库进行多次更新的问题
Calcium-Ion 1 rok temu
rodzic
commit
c76021e9a1
2 zmienionych plików z 24 dodań i 0 usunięć
  1. 11 0
      model/cache.go
  2. 13 0
      model/channel.go

+ 11 - 0
model/cache.go

@@ -342,3 +342,14 @@ func CacheGetChannel(id int) (*Channel, error) {
 	}
 	return c, nil
 }
+
+func CacheUpdateChannelStatus(id int, status int) {
+    if (!common.MemoryCacheEnabled) {
+        return
+    }
+    channelSyncLock.Lock()
+    defer channelSyncLock.Unlock()
+    if channel, ok := channelsIDM[id]; ok {
+        channel.Status = status
+    }
+}

+ 13 - 0
model/channel.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"one-api/common"
 	"strings"
+	"sync"
 
 	"gorm.io/gorm"
 )
@@ -290,7 +291,19 @@ func (channel *Channel) Delete() error {
 	return err
 }
 
+var channelStatusLock sync.Mutex
 func UpdateChannelStatusById(id int, status int, reason string) {
+	if (common.MemoryCacheEnabled) {
+		channelStatusLock.Lock()
+		channelCache, err := CacheGetChannel(id)
+		// 如果缓存渠道不存在或渠道已是目标状态,直接返回
+		if err != nil || channelCache.Status == status {
+			channelStatusLock.Unlock()
+			return
+		}
+		CacheUpdateChannelStatus(id, status)
+		channelStatusLock.Unlock()
+	}
 	err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
 	if err != nil {
 		common.SysError("failed to update ability status: " + err.Error())