Commit 24ae1fa5 by Calcium-Ion Committed by GitHub

Merge pull request #1240 from RedwindA/fix/redis

Fix: optimize Redis expiration handling and refactor cache duration retrieval
parents 28a0eb2a 09ffc364
...@@ -141,7 +141,11 @@ func RedisHSetObj(key string, obj interface{}, expiration time.Duration) error { ...@@ -141,7 +141,11 @@ func RedisHSetObj(key string, obj interface{}, expiration time.Duration) error {
txn := RDB.TxPipeline() txn := RDB.TxPipeline()
txn.HSet(ctx, key, data) txn.HSet(ctx, key, data)
txn.Expire(ctx, key, expiration)
// 只有在 expiration 大于 0 时才设置过期时间
if expiration > 0 {
txn.Expire(ctx, key, expiration)
}
_, err := txn.Exec(ctx) _, err := txn.Exec(ctx)
if err != nil { if err != nil {
......
...@@ -2,12 +2,10 @@ package constant ...@@ -2,12 +2,10 @@ package constant
import "one-api/common" import "one-api/common"
var ( // 使用函数来避免初始化顺序带来的赋值问题
TokenCacheSeconds = common.SyncFrequency func RedisKeyCacheSeconds() int {
UserId2GroupCacheSeconds = common.SyncFrequency return common.SyncFrequency
UserId2QuotaCacheSeconds = common.SyncFrequency }
UserId2StatusCacheSeconds = common.SyncFrequency
)
// Cache keys // Cache keys
const ( const (
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
func cacheSetToken(token Token) error { func cacheSetToken(token Token) error {
key := common.GenerateHMAC(token.Key) key := common.GenerateHMAC(token.Key)
token.Clean() token.Clean()
err := common.RedisHSetObj(fmt.Sprintf("token:%s", key), &token, time.Duration(constant.TokenCacheSeconds)*time.Second) err := common.RedisHSetObj(fmt.Sprintf("token:%s", key), &token, time.Duration(constant.RedisKeyCacheSeconds())*time.Second)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -70,7 +70,7 @@ func updateUserCache(user User) error { ...@@ -70,7 +70,7 @@ func updateUserCache(user User) error {
return common.RedisHSetObj( return common.RedisHSetObj(
getUserCacheKey(user.Id), getUserCacheKey(user.Id),
user.ToBaseUser(), user.ToBaseUser(),
time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second, time.Duration(constant.RedisKeyCacheSeconds())*time.Second,
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment