From afe9b8fdf3dfaa9462db9355f7f5f604a371ed3c Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Mon, 28 Oct 2024 16:43:47 +0800 Subject: [PATCH] fix: too many tokens --- pkg/common/db/cache/token.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/common/db/cache/token.go b/pkg/common/db/cache/token.go index 86d10d82..3ad6b6ca 100644 --- a/pkg/common/db/cache/token.go +++ b/pkg/common/db/cache/token.go @@ -27,7 +27,7 @@ import ( const ( chatToken = "CHAT_UID_TOKEN_STATUS:" - userMaxTokenNum = 20 + userMaxTokenNum = 10 ) type TokenInterface interface { @@ -96,15 +96,19 @@ func (t *TokenCacheRedis) SetTokenExpire(ctx context.Context, userID string, tok } } var sorted bool + var index int for i := len(mm) - len(fields); i > userMaxTokenNum; i-- { if !sorted { sorted = true sort.Sort(ts) } - fields = append(fields, ts[i].Token) + fields = append(fields, ts[index].Token) + index++ } - if err := t.rdb.HDel(ctx, key, fields...).Err(); err != nil { - return errs.Wrap(err) + if len(fields) > 0 { + if err := t.rdb.HDel(ctx, key, fields...).Err(); err != nil { + return errs.Wrap(err) + } } return nil }