From 739c0cc334ae64e3bf4b54112b559fe9cf25ba5f Mon Sep 17 00:00:00 2001 From: xqx333 <141810964+xqx333@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:10:05 +0800 Subject: [PATCH 1/2] Update cache.go --- model/cache.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/model/cache.go b/model/cache.go index 7f5411e2..a4ef47cd 100644 --- a/model/cache.go +++ b/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 + } +} From 173c9bc669a5865bd0a5e76a90478a50317317bd Mon Sep 17 00:00:00 2001 From: xqx333 <141810964+xqx333@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:11:24 +0800 Subject: [PATCH 2/2] Update channel.go --- model/channel.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/model/channel.go b/model/channel.go index af7c4279..a409d4d2 100644 --- a/model/channel.go +++ b/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())