add cacke

This commit is contained in:
李子铭 2025-03-20 17:44:14 +08:00
parent 71eb344f0c
commit 413b816a10
3 changed files with 69 additions and 7 deletions

View File

@ -25,6 +25,11 @@ const (
WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume" WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume"
) )
const (
ProductQueryKey CacheKey = "product_query"
ProductQueryLockKey CacheKey = "product_query_lock"
)
var CacheKeyMap = map[CacheKey]time.Duration{ var CacheKeyMap = map[CacheKey]time.Duration{
CmbOrderLockKey: 30 * time.Second, CmbOrderLockKey: 30 * time.Second,
CmbQueryLockKey: 30 * time.Second, CmbQueryLockKey: 30 * time.Second,
@ -37,6 +42,9 @@ var CacheKeyMap = map[CacheKey]time.Duration{
WechatNotifyRegisterTagCacheKey: 86400 * time.Second, // 1天 WechatNotifyRegisterTagCacheKey: 86400 * time.Second, // 1天
WechatNotifyRegisterTagCacheLockKey: 60 * time.Second, WechatNotifyRegisterTagCacheLockKey: 60 * time.Second,
WechatNotifyConsumeLockKey: 30 * time.Second, WechatNotifyConsumeLockKey: 30 * time.Second,
ProductQueryKey: 86400 * time.Second,
ProductQueryLockKey: 30 * time.Second,
} }
type Cache struct { type Cache struct {

View File

@ -90,7 +90,7 @@ func (p *OrderRepoImpl) GetByOutBizNo(ctx context.Context, t vo.OrderType, outBi
if tx.Error != nil { if tx.Error != nil {
sqlDB, _ := db.DB() sqlDB, _ := db.DB()
log.Warnf("order 当前打开连接数: %d ,空闲连接数: ", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle) log.Warnf("order当前打开连接数: %d,空闲连接数: %d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)
return nil, fmt.Errorf("order db fail %w", tx.Error) return nil, fmt.Errorf("order db fail %w", tx.Error)
} }

View File

@ -2,25 +2,30 @@ package repoimpl
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/redis/go-redis/v9"
"gorm.io/gorm" "gorm.io/gorm"
err2 "voucher/api/err" err2 "voucher/api/err"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/biz/repo" "voucher/internal/biz/repo"
"voucher/internal/biz/vo"
"voucher/internal/data" "voucher/internal/data"
"voucher/internal/data/model" "voucher/internal/data/model"
"voucher/internal/pkg/lock"
) )
// ProductRepoImpl . // ProductRepoImpl .
type ProductRepoImpl struct { type ProductRepoImpl struct {
Base[model.Product, bo.ProductBo] Base[model.Product, bo.ProductBo]
db *data.Db db *data.Db
rdb *data.Rdb
} }
// NewProductRepoImpl . // NewProductRepoImpl .
func NewProductRepoImpl(db *data.Db) repo.ProductRepo { func NewProductRepoImpl(db *data.Db, rdb *data.Rdb) repo.ProductRepo {
return &ProductRepoImpl{db: db} return &ProductRepoImpl{db: db, rdb: rdb}
} }
func (p *ProductRepoImpl) DB(ctx context.Context) *gorm.DB { func (p *ProductRepoImpl) DB(ctx context.Context) *gorm.DB {
@ -28,14 +33,63 @@ func (p *ProductRepoImpl) DB(ctx context.Context) *gorm.DB {
} }
func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.ProductBo, error) { func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.ProductBo, error) {
var item model.Product
var item *model.Product
c := vo.ProductQueryKey.BuildCache([]string{PNO})
cacheValue, err := r.rdb.Rdb.Get(ctx, c.Key).Result()
if err != nil && err != redis.Nil {
return nil, fmt.Errorf(fmt.Sprintf("获取商品缓存异常,%s:%v", c.Key, err))
}
if len(cacheValue) > 0 {
if err = json.Unmarshal([]byte(cacheValue), &item); err != nil {
return nil, err
}
return r.ToBo(item), nil
}
cl := vo.ProductQueryLockKey.BuildCache([]string{PNO})
err = lock.NewMutex(r.rdb.Rdb, cl.TTL).Lock(ctx, cl.Key, func(ctx context.Context) error {
cacheValue, err = r.rdb.Rdb.Get(ctx, c.Key).Result()
if err != nil && err != redis.Nil {
return fmt.Errorf(fmt.Sprintf("二次获取商品缓存异常,%s:%v", c.Key, err))
}
if len(cacheValue) > 0 {
return json.Unmarshal([]byte(cacheValue), &item)
}
item, err = r.getByPNO(ctx, item, PNO)
b, err := json.Marshal(item)
if err == nil {
r.rdb.Rdb.Set(ctx, c.Key, string(b), c.TTL)
}
return err
})
if err != nil {
return nil, err
}
return r.ToBo(item), err
}
func (r *ProductRepoImpl) getByPNO(ctx context.Context, item *model.Product, PNO string) (*model.Product, error) {
db := r.DB(ctx) db := r.DB(ctx)
tx := db.Where(model.Product{ProductNo: PNO}).Find(&item) tx := db.Where(model.Product{ProductNo: PNO}).Find(&item)
if tx.Error != nil { if tx.Error != nil {
sqlDB, _ := db.DB() sqlDB, _ := db.DB()
log.Warnf("当前打开连接数: %d ,空闲连接数: ", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle) log.Warnf("product当前打开连接数: %d,空闲连接数: %d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)
return nil, fmt.Errorf("product db fail %w", tx.Error) return nil, fmt.Errorf("product db fail %w", tx.Error)
} }
@ -44,5 +98,5 @@ func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.Product
return nil, err2.ErrorDbNotFound("商品数据不存在") return nil, err2.ErrorDbNotFound("商品数据不存在")
} }
return r.ToBo(&item), nil return item, nil
} }