refactor: Optimize user caching and token retrieval methods
This commit is contained in:
@@ -52,7 +52,7 @@ func cacheSetTokenField(key string, field string, value string) error {
|
||||
func cacheGetTokenByKey(key string) (*Token, error) {
|
||||
hmacKey := common.GenerateHMAC(key)
|
||||
if !common.RedisEnabled {
|
||||
return nil, nil
|
||||
return nil, fmt.Errorf("redis is not enabled")
|
||||
}
|
||||
var token Token
|
||||
err := common.RedisHGetObj(fmt.Sprintf("token:%s", hmacKey), &token)
|
||||
|
||||
@@ -42,8 +42,8 @@ type User struct {
|
||||
Setting string `json:"setting" gorm:"type:text;column:setting"`
|
||||
}
|
||||
|
||||
func (user *User) ToBaseUser() UserBase {
|
||||
cache := UserBase{
|
||||
func (user *User) ToBaseUser() *UserBase {
|
||||
cache := &UserBase{
|
||||
Id: user.Id,
|
||||
Group: user.Group,
|
||||
Quota: user.Quota,
|
||||
|
||||
@@ -79,7 +79,7 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
|
||||
}()
|
||||
|
||||
// Try getting from Redis first
|
||||
err = common.RedisHGetObj(getUserCacheKey(userId), &userCache)
|
||||
userCache, err = cacheGetUserBase(userId)
|
||||
if err == nil {
|
||||
return userCache, nil
|
||||
}
|
||||
@@ -105,6 +105,19 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
|
||||
return userCache, nil
|
||||
}
|
||||
|
||||
func cacheGetUserBase(userId int) (*UserBase, error) {
|
||||
if !common.RedisEnabled {
|
||||
return nil, fmt.Errorf("redis is not enabled")
|
||||
}
|
||||
var userCache UserBase
|
||||
// Try getting from Redis first
|
||||
err := common.RedisHGetObj(getUserCacheKey(userId), &userCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &userCache, nil
|
||||
}
|
||||
|
||||
// Add atomic quota operations using hash fields
|
||||
func cacheIncrUserQuota(userId int, delta int64) error {
|
||||
if !common.RedisEnabled {
|
||||
|
||||
Reference in New Issue
Block a user