refactor: Optimize user caching and token retrieval methods
This commit is contained in:
@@ -17,7 +17,7 @@ func GetPricing(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
var group string
|
var group string
|
||||||
if exists {
|
if exists {
|
||||||
user, err := model.GetUserById(userId.(int), false)
|
user, err := model.GetUserCache(userId.(int))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
group = user.Group
|
group = user.Group
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -472,7 +472,7 @@ func GetUserModels(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
id = c.GetInt("id")
|
id = c.GetInt("id")
|
||||||
}
|
}
|
||||||
user, err := model.GetUserById(id, true)
|
user, err := model.GetUserCache(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func cacheSetTokenField(key string, field string, value string) error {
|
|||||||
func cacheGetTokenByKey(key string) (*Token, error) {
|
func cacheGetTokenByKey(key string) (*Token, error) {
|
||||||
hmacKey := common.GenerateHMAC(key)
|
hmacKey := common.GenerateHMAC(key)
|
||||||
if !common.RedisEnabled {
|
if !common.RedisEnabled {
|
||||||
return nil, nil
|
return nil, fmt.Errorf("redis is not enabled")
|
||||||
}
|
}
|
||||||
var token Token
|
var token Token
|
||||||
err := common.RedisHGetObj(fmt.Sprintf("token:%s", hmacKey), &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"`
|
Setting string `json:"setting" gorm:"type:text;column:setting"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) ToBaseUser() UserBase {
|
func (user *User) ToBaseUser() *UserBase {
|
||||||
cache := UserBase{
|
cache := &UserBase{
|
||||||
Id: user.Id,
|
Id: user.Id,
|
||||||
Group: user.Group,
|
Group: user.Group,
|
||||||
Quota: user.Quota,
|
Quota: user.Quota,
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Try getting from Redis first
|
// Try getting from Redis first
|
||||||
err = common.RedisHGetObj(getUserCacheKey(userId), &userCache)
|
userCache, err = cacheGetUserBase(userId)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return userCache, nil
|
return userCache, nil
|
||||||
}
|
}
|
||||||
@@ -105,6 +105,19 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
|
|||||||
return userCache, nil
|
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
|
// Add atomic quota operations using hash fields
|
||||||
func cacheIncrUserQuota(userId int, delta int64) error {
|
func cacheIncrUserQuota(userId int, delta int64) error {
|
||||||
if !common.RedisEnabled {
|
if !common.RedisEnabled {
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ func PreConsumeTokenQuota(relayInfo *relaycommon.RelayInfo, quota int) error {
|
|||||||
//if relayInfo.TokenUnlimited {
|
//if relayInfo.TokenUnlimited {
|
||||||
// return nil
|
// return nil
|
||||||
//}
|
//}
|
||||||
token, err := model.GetTokenById(relayInfo.TokenId)
|
token, err := model.GetTokenByKey(relayInfo.TokenKey, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func NotifyRootUser(t string, subject string, content string) {
|
func NotifyRootUser(t string, subject string, content string) {
|
||||||
user := model.GetRootUser().ToBaseUser()
|
user := model.GetRootUser().ToBaseUser()
|
||||||
_ = NotifyUser(&user, dto.NewNotify(t, subject, content, nil))
|
_ = NotifyUser(user, dto.NewNotify(t, subject, content, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotifyUser(user *model.UserBase, data dto.Notify) error {
|
func NotifyUser(user *model.UserBase, data dto.Notify) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user