From f5fa1c4ed3723b602a46f75056e0e6a0b64b345a Mon Sep 17 00:00:00 2001 From: icey-yu <1186114839@qq.com> Date: Thu, 13 Jun 2024 12:23:03 +0800 Subject: [PATCH] refactor:move expire time calculate into cache from controller --- internal/rpc/auth/auth.go | 2 +- pkg/common/storage/cache/redis/token.go | 19 ++++++++++++------- pkg/common/storage/cache/token.go | 3 +-- pkg/common/storage/controller/auth.go | 7 +------ 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/internal/rpc/auth/auth.go b/internal/rpc/auth/auth.go index 6270b39b3..b1d603739 100644 --- a/internal/rpc/auth/auth.go +++ b/internal/rpc/auth/auth.go @@ -61,7 +61,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg userRpcClient: &userRpcClient, RegisterCenter: client, authDatabase: controller.NewAuthDatabase( - redis2.NewTokenCacheModel(rdb), + redis2.NewTokenCacheModel(rdb, config.RpcConfig.TokenPolicy.Expire), config.Share.Secret, config.RpcConfig.TokenPolicy.Expire, ), diff --git a/pkg/common/storage/cache/redis/token.go b/pkg/common/storage/cache/redis/token.go index cc49795e6..b82259658 100644 --- a/pkg/common/storage/cache/redis/token.go +++ b/pkg/common/storage/cache/redis/token.go @@ -25,13 +25,14 @@ import ( ) type tokenCache struct { - rdb redis.UniversalClient + rdb redis.UniversalClient + accessExpire time.Duration } -func NewTokenCacheModel(rdb redis.UniversalClient) cache.TokenModel { - return &tokenCache{ - rdb: rdb, - } +func NewTokenCacheModel(rdb redis.UniversalClient, accessExpire int64) cache.TokenModel { + c := &tokenCache{rdb: rdb} + c.accessExpire = c.getExpireTime(accessExpire) + return c } func (c *tokenCache) SetTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error { @@ -39,12 +40,12 @@ func (c *tokenCache) SetTokenFlag(ctx context.Context, userID string, platformID } // SetTokenFlagEx set token and flag with expire time -func (c *tokenCache) SetTokenFlagEx(ctx context.Context, userID string, platformID int, token string, flag int, expire time.Duration) error { +func (c *tokenCache) SetTokenFlagEx(ctx context.Context, userID string, platformID int, token string, flag int) error { key := cachekey.GetTokenKey(userID, platformID) if err := c.rdb.HSet(ctx, key, token, flag).Err(); err != nil { return errs.Wrap(err) } - if err := c.rdb.Expire(ctx, key, expire).Err(); err != nil { + if err := c.rdb.Expire(ctx, key, c.accessExpire).Err(); err != nil { return errs.Wrap(err) } return nil @@ -74,3 +75,7 @@ func (c *tokenCache) SetTokenMapByUidPid(ctx context.Context, userID string, pla func (c *tokenCache) DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error { return errs.Wrap(c.rdb.HDel(ctx, cachekey.GetTokenKey(userID, platformID), fields...).Err()) } + +func (c *tokenCache) getExpireTime(t int64) time.Duration { + return time.Hour * 24 * time.Duration(t) +} diff --git a/pkg/common/storage/cache/token.go b/pkg/common/storage/cache/token.go index 3b7aa80e1..4a0fee087 100644 --- a/pkg/common/storage/cache/token.go +++ b/pkg/common/storage/cache/token.go @@ -2,13 +2,12 @@ package cache import ( "context" - "time" ) type TokenModel interface { SetTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error // SetTokenFlagEx set token and flag with expire time - SetTokenFlagEx(ctx context.Context, userID string, platformID int, token string, flag int, expire time.Duration) error + SetTokenFlagEx(ctx context.Context, userID string, platformID int, token string, flag int) error GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error diff --git a/pkg/common/storage/controller/auth.go b/pkg/common/storage/controller/auth.go index f77a7a1fa..fbfe30836 100644 --- a/pkg/common/storage/controller/auth.go +++ b/pkg/common/storage/controller/auth.go @@ -16,7 +16,6 @@ package controller import ( "context" - "time" "github.com/golang-jwt/jwt/v4" "github.com/openimsdk/open-im-server/v3/pkg/authverify" @@ -87,7 +86,7 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI if isCreate { // should create,should specify expiration time - if err = a.cache.SetTokenFlagEx(ctx, userID, platformID, tokenString, constant.NormalToken, a.getExpireTime()); err != nil { + if err = a.cache.SetTokenFlagEx(ctx, userID, platformID, tokenString, constant.NormalToken); err != nil { return "", err } } else { @@ -98,7 +97,3 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI } return tokenString, nil } - -func (a *authDatabase) getExpireTime() time.Duration { - return time.Hour * 24 * time.Duration(a.accessExpire) -}