From 27743f103a0bf0b603a6a5afb21a8bc88317ce6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Fri, 7 Mar 2025 18:28:44 +0800 Subject: [PATCH] cmb --- cmd/server/main.go | 4 ++++ internal/biz/cmb.go | 9 ++++++--- internal/biz/vo/cache.go | 8 +++++++- internal/biz/wechat_notify_consume.go | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index ddfe0b1..e33450d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -116,6 +116,10 @@ func loadConfig() *conf.Bootstrap { func main() { bc := loadConfig() + if bc == nil { + panic("配置文件加载失败") + } + businessLogger := log2.NewBusinessLogger(bc.Logs.Business, Name, Name, Version) accessLogger := log2.NewAccessLogger(bc.Logs.Business, id, Name, Version) diff --git a/internal/biz/cmb.go b/internal/biz/cmb.go index 12010ba..d139fee 100644 --- a/internal/biz/cmb.go +++ b/internal/biz/cmb.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "gorm.io/gorm" - "time" v1 "voucher/api/v1" "voucher/internal/biz/bo" "voucher/internal/biz/vo" @@ -14,7 +13,9 @@ import ( func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) { - err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_order_%s", req.OutBizNo), func(ctx context.Context) error { + c := vo.CmbOrderLockKey.BuildCache([]string{req.OutBizNo}) + + err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { order, err := v.OrderRepo.GetByOutBizNo(ctx, req.OutBizNo) @@ -48,7 +49,9 @@ func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (or func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (reps *v1.CmbQueryProductReply, err error) { - err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_product_query_%s", productNo), func(ctx context.Context) error { + c := vo.CmbProductQueryLockKey.BuildCache([]string{productNo}) + + err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { product, err := v.ProductRepo.GetByPNO(ctx, productNo) if err != nil { diff --git a/internal/biz/vo/cache.go b/internal/biz/vo/cache.go index e015435..6e972e6 100644 --- a/internal/biz/vo/cache.go +++ b/internal/biz/vo/cache.go @@ -8,15 +8,21 @@ import ( type CacheKey string const ( + CmbOrderLockKey CacheKey = "cmb_order" + CmbProductQueryLockKey CacheKey = "cmb_product_query" + WechatNotifyRegisterTagCacheKey CacheKey = "wechat_notify_register_tag" WechatNotifyRegisterTagCacheLockKey CacheKey = "wechat_notify_register_tag_lock" - WechatNotifyConsumeKey CacheKey = "wechat_notify_consume" + WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume" ) var CacheKeyMap = map[CacheKey]time.Duration{ + CmbOrderLockKey: 30 * time.Second, + CmbProductQueryLockKey: 30 * time.Second, WechatNotifyRegisterTagCacheKey: 86400 * time.Second, WechatNotifyRegisterTagCacheLockKey: 30 * time.Second, + WechatNotifyConsumeLockKey: 30 * time.Second, } type Cache struct { diff --git a/internal/biz/wechat_notify_consume.go b/internal/biz/wechat_notify_consume.go index ee3a436..4b7d9bf 100644 --- a/internal/biz/wechat_notify_consume.go +++ b/internal/biz/wechat_notify_consume.go @@ -10,7 +10,7 @@ import ( ) func (j *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *bo.WechatVoucherNotifyBo) error { - c := vo.WechatNotifyConsumeKey.BuildCache([]string{tag, req.PlainText.StockID, req.PlainText.CouponID}) + c := vo.WechatNotifyConsumeLockKey.BuildCache([]string{tag, req.PlainText.StockID, req.PlainText.CouponID}) return lock.NewMutex(j.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {