GetUserQuota gets quota from Redis first, falls back to DB if needed
(id int, fromDB bool)
| 567 | |
| 568 | // GetUserQuota gets quota from Redis first, falls back to DB if needed |
| 569 | func GetUserQuota(id int, fromDB bool) (quota int, err error) { |
| 570 | defer func() { |
| 571 | // Update Redis cache asynchronously on successful DB read |
| 572 | if shouldUpdateRedis(fromDB, err) { |
| 573 | gopool.Go(func() { |
| 574 | if err := updateUserQuotaCache(id, quota); err != nil { |
| 575 | common.SysError("failed to update user quota cache: " + err.Error()) |
| 576 | } |
| 577 | }) |
| 578 | } |
| 579 | }() |
| 580 | if !fromDB && common.RedisEnabled { |
| 581 | quota, err := getUserQuotaCache(id) |
| 582 | if err == nil { |
| 583 | return quota, nil |
| 584 | } |
| 585 | // Don't return error - fall through to DB |
| 586 | } |
| 587 | fromDB = true |
| 588 | err = DB.Model(&User{}).Where("id = ?", id).Select("quota").Find("a).Error |
| 589 | if err != nil { |
| 590 | return 0, err |
| 591 | } |
| 592 | |
| 593 | return quota, nil |
| 594 | } |
| 595 | |
| 596 | func GetUserUsedQuota(id int) (quota int, err error) { |
| 597 | err = DB.Model(&User{}).Where("id = ?", id).Select("used_quota").Find("a).Error |
no test coverage detected