diff --git a/model/cache.go b/model/cache.go index dc2ed3b..2080292 100644 --- a/model/cache.go +++ b/model/cache.go @@ -168,7 +168,11 @@ func CacheUpdateUserQuota(id int) error { if err != nil { return err } - err = common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(UserId2QuotaCacheSeconds)*time.Second) + return CacheSetUserQuota(id, quota) +} + +func CacheSetUserQuota(id int, quota int) error { + err := common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(UserId2QuotaCacheSeconds)*time.Second) return err } diff --git a/model/token.go b/model/token.go index 08909cb..51805bc 100644 --- a/model/token.go +++ b/model/token.go @@ -102,6 +102,9 @@ func GetTokenById(id int) (*Token, error) { token := Token{Id: id} var err error = nil err = DB.First(&token, "id = ?", id).Error + if err != nil { + go cacheSetToken(&token) + } return &token, err } diff --git a/model/user.go b/model/user.go index 8aaaf52..ac1f8d3 100644 --- a/model/user.go +++ b/model/user.go @@ -410,6 +410,11 @@ func ValidateAccessToken(token string) (user *User) { func GetUserQuota(id int) (quota int, err error) { err = DB.Model(&User{}).Where("id = ?", id).Select("quota").Find("a).Error + if err != nil { + if common.RedisEnabled { + go CacheSetUserQuota(id, quota) + } + } return quota, err }