diff --git a/common/redis.go b/common/redis.go index ba35331a..1efc217f 100644 --- a/common/redis.go +++ b/common/redis.go @@ -141,7 +141,11 @@ func RedisHSetObj(key string, obj interface{}, expiration time.Duration) error { txn := RDB.TxPipeline() txn.HSet(ctx, key, data) - txn.Expire(ctx, key, expiration) + + // 只有在 expiration 大于 0 时才设置过期时间 + if expiration > 0 { + txn.Expire(ctx, key, expiration) + } _, err := txn.Exec(ctx) if err != nil { diff --git a/constant/cache_key.go b/constant/cache_key.go index 27cb3b75..daedfd40 100644 --- a/constant/cache_key.go +++ b/constant/cache_key.go @@ -2,12 +2,10 @@ package constant import "one-api/common" -var ( - TokenCacheSeconds = common.SyncFrequency - UserId2GroupCacheSeconds = common.SyncFrequency - UserId2QuotaCacheSeconds = common.SyncFrequency - UserId2StatusCacheSeconds = common.SyncFrequency -) +// 使用函数来避免初始化顺序带来的赋值问题 +func RedisKeyCacheSeconds() int { + return common.SyncFrequency +} // Cache keys const ( diff --git a/model/token_cache.go b/model/token_cache.go index b2e0c951..a4b0beae 100644 --- a/model/token_cache.go +++ b/model/token_cache.go @@ -10,7 +10,7 @@ import ( func cacheSetToken(token Token) error { key := common.GenerateHMAC(token.Key) 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 { return err } diff --git a/model/user_cache.go b/model/user_cache.go index d74877bd..e673defc 100644 --- a/model/user_cache.go +++ b/model/user_cache.go @@ -70,7 +70,7 @@ func updateUserCache(user User) error { return common.RedisHSetObj( getUserCacheKey(user.Id), user.ToBaseUser(), - time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second, + time.Duration(constant.RedisKeyCacheSeconds())*time.Second, ) }