fix: when cached quota is too low, force refresh it

This commit is contained in:
JustSong 2024-03-13 19:26:24 +08:00
parent 49cad7d4a5
commit 0710f8cd66

View File

@ -70,12 +70,7 @@ func CacheGetUserGroup(id int) (group string, err error) {
return group, err
}
func CacheGetUserQuota(id int) (quota int, err error) {
if !common.RedisEnabled {
return GetUserQuota(id)
}
quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id))
if err != nil {
func fetchAndUpdateUserQuota(id int) (quota int, err error) {
quota, err = GetUserQuota(id)
if err != nil {
return 0, err
@ -84,9 +79,21 @@ func CacheGetUserQuota(id int) (quota int, err error) {
if err != nil {
logger.SysError("Redis set user quota error: " + err.Error())
}
return quota, err
return
}
func CacheGetUserQuota(id int) (quota int, err error) {
if !common.RedisEnabled {
return GetUserQuota(id)
}
quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id))
if err != nil {
return fetchAndUpdateUserQuota(id)
}
quota, err = strconv.Atoi(quotaString)
if quota <= config.PreConsumedQuota { // when user's quota is less than pre-consumed quota, we need to fetch from db
return fetchAndUpdateUserQuota(id)
}
return quota, err
}