refactor:move expire time calculate into cache from controller

pull/2352/head
icey-yu 1 year ago
parent b2824a92c3
commit f5fa1c4ed3

@ -61,7 +61,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
userRpcClient: &userRpcClient, userRpcClient: &userRpcClient,
RegisterCenter: client, RegisterCenter: client,
authDatabase: controller.NewAuthDatabase( authDatabase: controller.NewAuthDatabase(
redis2.NewTokenCacheModel(rdb), redis2.NewTokenCacheModel(rdb, config.RpcConfig.TokenPolicy.Expire),
config.Share.Secret, config.Share.Secret,
config.RpcConfig.TokenPolicy.Expire, config.RpcConfig.TokenPolicy.Expire,
), ),

@ -25,13 +25,14 @@ import (
) )
type tokenCache struct { type tokenCache struct {
rdb redis.UniversalClient rdb redis.UniversalClient
accessExpire time.Duration
} }
func NewTokenCacheModel(rdb redis.UniversalClient) cache.TokenModel { func NewTokenCacheModel(rdb redis.UniversalClient, accessExpire int64) cache.TokenModel {
return &tokenCache{ c := &tokenCache{rdb: rdb}
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 { 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 // 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) key := cachekey.GetTokenKey(userID, platformID)
if err := c.rdb.HSet(ctx, key, token, flag).Err(); err != nil { if err := c.rdb.HSet(ctx, key, token, flag).Err(); err != nil {
return errs.Wrap(err) 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 errs.Wrap(err)
} }
return nil 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 { 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()) 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)
}

@ -2,13 +2,12 @@ package cache
import ( import (
"context" "context"
"time"
) )
type TokenModel interface { type TokenModel interface {
SetTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error SetTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
// SetTokenFlagEx set token and flag with expire time // 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) 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 SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error

@ -16,7 +16,6 @@ package controller
import ( import (
"context" "context"
"time"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"github.com/openimsdk/open-im-server/v3/pkg/authverify" "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 { if isCreate {
// should create,should specify expiration time // 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 return "", err
} }
} else { } else {
@ -98,7 +97,3 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
} }
return tokenString, nil return tokenString, nil
} }
func (a *authDatabase) getExpireTime() time.Duration {
return time.Hour * 24 * time.Duration(a.accessExpire)
}

Loading…
Cancel
Save