Skip to content

Commit

Permalink
fix: too many tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
withchao committed Oct 28, 2024
1 parent bdbb4d4 commit afe9b8f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/common/db/cache/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

const (
chatToken = "CHAT_UID_TOKEN_STATUS:"
userMaxTokenNum = 20
userMaxTokenNum = 10
)

type TokenInterface interface {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit afe9b8f

Please sign in to comment.