del cache code
This commit is contained in:
parent
8f3ca4b199
commit
e1fc391984
|
|
@ -3,7 +3,6 @@ package biz
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
err2 "voucher/api/err"
|
err2 "voucher/api/err"
|
||||||
"voucher/internal/biz/bo"
|
"voucher/internal/biz/bo"
|
||||||
"voucher/internal/biz/vo"
|
"voucher/internal/biz/vo"
|
||||||
|
|
@ -23,33 +22,9 @@ func (this *VoucherBiz) RegisterTag(ctx context.Context, productNo string) error
|
||||||
|
|
||||||
func (v *VoucherBiz) registerNotifyTag(ctx context.Context, stockCreatorMchID, stockID string) error {
|
func (v *VoucherBiz) registerNotifyTag(ctx context.Context, stockCreatorMchID, stockID string) error {
|
||||||
|
|
||||||
c := vo.WechatNotifyRegisterTagCacheKey.BuildCache([]string{v.bc.WechatNotifyMQ.Tag, stockCreatorMchID, stockID})
|
|
||||||
|
|
||||||
_, err := v.rdb.Rdb.Get(ctx, c.Key).Result()
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
// 缓存存在,直接返回
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != redis.Nil {
|
|
||||||
return fmt.Errorf(fmt.Sprintf("获取redis缓存%s异常:%v", c.Key, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
cl := vo.WechatNotifyRegisterTagCacheLockKey.BuildCache([]string{v.bc.WechatNotifyMQ.Tag, stockCreatorMchID, stockID})
|
cl := vo.WechatNotifyRegisterTagCacheLockKey.BuildCache([]string{v.bc.WechatNotifyMQ.Tag, stockCreatorMchID, stockID})
|
||||||
|
|
||||||
return lock.NewMutex(v.rdb.Rdb, cl.TTL).Lock(ctx, cl.Key, func(ctx context.Context) error {
|
return lock.NewMutex(v.rdb.Rdb, cl.TTL).Lock(ctx, cl.Key, func(ctx context.Context) error {
|
||||||
// 二次获取,判定处理,以免获取锁后又执行了一次
|
|
||||||
|
|
||||||
cacheValue, err3 := v.rdb.Rdb.Get(ctx, c.Key).Result()
|
|
||||||
|
|
||||||
if err3 != nil && err3 != redis.Nil {
|
|
||||||
return fmt.Errorf(fmt.Sprintf("二次获取redis缓存%s异常:%v", c.Key, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
if cacheValue != "" {
|
|
||||||
return nil // 有直接返回
|
|
||||||
}
|
|
||||||
|
|
||||||
wechatNotifyTag, err3 := v.WechatNotifyRegisterTagRepo.GetByStockIdAndMchId(ctx, stockCreatorMchID, stockID)
|
wechatNotifyTag, err3 := v.WechatNotifyRegisterTagRepo.GetByStockIdAndMchId(ctx, stockCreatorMchID, stockID)
|
||||||
if err3 != nil && !err2.IsDbNotFound(err3) {
|
if err3 != nil && !err2.IsDbNotFound(err3) {
|
||||||
|
|
@ -62,7 +37,7 @@ func (v *VoucherBiz) registerNotifyTag(ctx context.Context, stockCreatorMchID, s
|
||||||
}
|
}
|
||||||
|
|
||||||
if wechatNotifyTag.Status.IsSuccess() {
|
if wechatNotifyTag.Status.IsSuccess() {
|
||||||
return v.setCache(ctx, c, wechatNotifyTag)
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wechatNotifyTag, err3 = v.createWechatNotifyRegisterTag(ctx, stockCreatorMchID, stockID)
|
wechatNotifyTag, err3 = v.createWechatNotifyRegisterTag(ctx, stockCreatorMchID, stockID)
|
||||||
|
|
@ -71,16 +46,11 @@ func (v *VoucherBiz) registerNotifyTag(ctx context.Context, stockCreatorMchID, s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = v.WechatCpnRepo.RegisterNotifyTag(ctx, stockID); err != nil {
|
if err := v.WechatCpnRepo.RegisterNotifyTag(ctx, stockID); err != nil {
|
||||||
|
|
||||||
return v.WechatNotifyRegisterTagRepo.Fail(ctx, wechatNotifyTag.ID, err.Error())
|
return v.WechatNotifyRegisterTagRepo.Fail(ctx, wechatNotifyTag.ID, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = v.WechatNotifyRegisterTagRepo.Success(ctx, wechatNotifyTag.ID); err != nil {
|
return v.WechatNotifyRegisterTagRepo.Success(ctx, wechatNotifyTag.ID)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return v.setCache(ctx, c, wechatNotifyTag)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,12 +61,3 @@ func (v *VoucherBiz) createWechatNotifyRegisterTag(ctx context.Context, stockCre
|
||||||
Tag: v.bc.WechatNotifyMQ.Tag,
|
Tag: v.bc.WechatNotifyMQ.Tag,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VoucherBiz) setCache(ctx context.Context, c *vo.Cache, wechatNotifyTag *bo.WechatNotifyRegisterTagBo) error {
|
|
||||||
|
|
||||||
if err := v.rdb.Rdb.Set(ctx, c.Key, wechatNotifyTag.Tag, c.TTL).Err(); err != nil {
|
|
||||||
return fmt.Errorf(fmt.Sprintf("设置redis缓存%s异常:%v", c.Key, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ var CacheKeyMap = map[CacheKey]time.Duration{
|
||||||
WechatNotifyRegisterTagCacheLockKey: 60 * time.Second,
|
WechatNotifyRegisterTagCacheLockKey: 60 * time.Second,
|
||||||
WechatNotifyConsumeLockKey: 30 * time.Second,
|
WechatNotifyConsumeLockKey: 30 * time.Second,
|
||||||
|
|
||||||
ProductQueryKey: 15 * 86400 * time.Second, // 15天
|
ProductQueryKey: 30 * 86400 * time.Second, // 30天
|
||||||
ProductQueryLockKey: 30 * time.Second,
|
ProductQueryLockKey: 30 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue