enhance(charge):为订阅费用管理增加缓存控制功能。

This commit is contained in:
徐涛 2022-08-25 21:24:06 +08:00
parent 8d3ce16ce0
commit 4f722f2701
2 changed files with 46 additions and 22 deletions

View File

@ -1,10 +1,13 @@
package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/exceptions"
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"fmt"
"strconv"
"time"
"github.com/fufuok/utils"
@ -18,18 +21,6 @@ type _ChargeService struct{}
var ChargeService _ChargeService
func (c _ChargeService) CreateChargeRecord(charge *model.UserCharge, extendWithIgnoreSettle bool) error {
// var seqs = make([]int64, 0)
// err := global.DBConn.Table(&model.UserCharge{}).Cols("seq").Find(&seqs)
// if err != nil {
// return err
// }
// maxSeq := utils.Reduce(seqs, 0, func(acc, elem int64) int64 {
// if elem > acc {
// return elem
// } else {
// return acc
// }
// })
tx := global.DBConn.NewSession()
defer tx.Close()
if err := tx.Begin(); err != nil {
@ -52,6 +43,7 @@ func (c _ChargeService) CreateChargeRecord(charge *model.UserCharge, extendWithI
tx.Rollback()
return err
}
cache.AbolishRelation("charge")
return nil
}
@ -84,6 +76,8 @@ func (c _ChargeService) SettleCharge(seq int64, uid string) error {
tx.Rollback()
return err
}
cache.AbolishRelation("charge")
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
return nil
}
@ -117,6 +111,8 @@ func (c _ChargeService) RefundCharge(seq int64, uid string) error {
tx.Rollback()
return err
}
cache.AbolishRelation("charge")
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
return nil
}
@ -159,6 +155,8 @@ func (c _ChargeService) CancelCharge(seq int64, uid string) error {
tx.Rollback()
return err
}
cache.AbolishRelation("charge")
cache.AbolishRelation(fmt.Sprintf("charge_%s_%d", uid, seq))
return nil
}
@ -167,13 +165,20 @@ func (_ChargeService) updateUserExpiration(tx *xorm.Session, uid string, expirat
if err != nil {
tx.Rollback()
}
cache.AbolishRelation("user")
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
return err
}
func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string, page int) ([]model.ChargeWithName, int64, error) {
var cond = builder.NewCond()
var (
cond = builder.NewCond()
condition = make([]string, 0)
)
condition = append(condition, strconv.Itoa(page))
if len(keyword) != 0 {
cond = cond.And(builder.Like{"d.name", keyword}.Or(builder.Like{"d.abbr", keyword}))
condition = append(condition, keyword)
}
if len(beginDate) != 0 {
beginTime, err := time.ParseInLocation("2006-01-02", beginDate, time.Local)
@ -182,6 +187,7 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
return make([]model.ChargeWithName, 0), 0, err
}
cond = cond.And(builder.Gte{"c.created_at": beginTime})
condition = append(condition, strconv.FormatInt(beginTime.Unix(), 10))
}
if len(endDate) != 0 {
endTime, err := time.ParseInLocation("2006-01-02", endDate, time.Local)
@ -190,18 +196,31 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
return make([]model.ChargeWithName, 0), 0, err
}
cond = cond.And(builder.Lte{"c.created_at": endTime})
condition = append(condition, strconv.FormatInt(endTime.Unix(), 10))
}
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
total, err := global.DBConn.
Alias("d").
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
Where(cond).
NoAutoCondition().
Count(&model.ChargeWithName{})
if err != nil {
return nil, -1, err
var (
total int64
err error
)
if cachedTotal, _ := cache.RetreiveCount("charge_with_name", condition...); cachedTotal != -1 {
total = cachedTotal
} else {
total, err = global.DBConn.
Alias("d").
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
Where(cond).
NoAutoCondition().
Count(&model.ChargeWithName{})
if err != nil {
return nil, -1, err
}
cache.CacheCount("charge", "charge_with_name", total, condition...)
}
charges := make([]model.ChargeWithName, 0)
if cachedCharges, _ := cache.RetreiveSearch[[]model.ChargeWithName]("charge_with_name", condition...); cachedCharges != nil {
return *cachedCharges, total, nil
}
err = global.DBConn.
Alias("d").
Join("INNER", []string{"user_charge", "c"}, "c.user_id=d.id").
@ -209,10 +228,14 @@ func (_ChargeService) ListPagedChargeRecord(keyword, beginDate, endDate string,
Limit(config.ServiceSettings.ItemsPageSize, startItem).
NoAutoCondition().
Find(&charges)
cache.CacheSearch(charges, "charge", "charge_with_name", condition...)
return charges, total, err
}
func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
if cachedValid, _ := cache.RetreiveSearch[time.Time]("last_valid_charge", uid); cachedValid != nil {
return *cachedValid, nil
}
veryBlankTime, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
var records []string
err := global.DBConn.
@ -234,5 +257,6 @@ func (_ChargeService) lastValidChargeTo(uid string) (time.Time, error) {
return acc
}
}, veryBlankTime)
cache.CacheSearch(lastValid, "charge", "last_valid_charge", uid)
return lastValid, nil
}

View File

@ -262,7 +262,7 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
if err != nil {
return make([]model.JoinedUserDetail, 0), err
}
cache.CacheSearch(users, "user", "join_user_detail", strconv.Itoa(limit))
cache.CacheSearch(users, "user", "join_user_detail", keyword, strconv.Itoa(limit))
return users, nil
}