From 70bdb8ca9049fbfaa0e4fb76ddcb008c6555446e Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Fri, 26 Jan 2024 15:52:23 +0800 Subject: [PATCH] fix: fix redis error --- common/redis.go | 5 +++++ model/cache.go | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/common/redis.go b/common/redis.go index 3cd45db..54a7cb3 100644 --- a/common/redis.go +++ b/common/redis.go @@ -57,6 +57,11 @@ func RedisGet(key string) (string, error) { return RDB.Get(ctx, key).Result() } +func RedisExpire(key string, expiration time.Duration) error { + ctx := context.Background() + return RDB.Expire(ctx, key, expiration).Err() +} + func RedisGetEx(key string, expiration time.Duration) (string, error) { ctx := context.Background() return RDB.GetSet(ctx, key, expiration).Result() diff --git a/model/cache.go b/model/cache.go index 1db3021..43c97eb 100644 --- a/model/cache.go +++ b/model/cache.go @@ -49,7 +49,7 @@ func CacheGetTokenByKey(key string) (*Token, error) { return GetTokenByKey(key) } var token *Token - tokenObjectString, err := common.RedisGetEx(fmt.Sprintf("token:%s", key), time.Duration(TokenCacheSeconds)*time.Second) + tokenObjectString, err := common.RedisGet(fmt.Sprintf("token:%s", key)) if err != nil { // 如果缓存中不存在,则从数据库中获取 token, err = GetTokenByKey(key) @@ -59,6 +59,8 @@ func CacheGetTokenByKey(key string) (*Token, error) { err = cacheSetToken(token) return token, nil } + // 如果缓存中存在,则续期时间 + err = common.RedisExpire(fmt.Sprintf("token:%s", key), time.Duration(TokenCacheSeconds)*time.Second) err = json.Unmarshal([]byte(tokenObjectString), &token) return token, err }