refactor: 将分布式锁从 redis 迁移至 utils 包
This commit is contained in:
@@ -82,7 +82,7 @@ func (d *category) List(ctx context.Context, req *dto.ListCategoryReq, fields ..
|
|||||||
|
|
||||||
// buildListFilter 构建列表查询的过滤条件
|
// buildListFilter 构建列表查询的过滤条件
|
||||||
func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq) *gdb.Model {
|
func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq) *gdb.Model {
|
||||||
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Model
|
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Cache(ctx)
|
||||||
if !g.IsEmpty(req.Keyword) {
|
if !g.IsEmpty(req.Keyword) {
|
||||||
model.WhereLike(entity.CategoryCol.Name, "%"+req.Keyword+"%")
|
model.WhereLike(entity.CategoryCol.Name, "%"+req.Keyword+"%")
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -27,7 +27,7 @@ func main() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
defer jaeger.ShutDown(ctx)
|
defer jaeger.ShutDown(ctx)
|
||||||
// 注册路由
|
// 注册路由
|
||||||
// http.Httpserver.BindMiddleware("/*", middleware.ModuleTenantCheck)
|
//http.Httpserver.BindMiddleware("/*", http.SkipMiddleware(middleware.ModuleTenantCheck, "/*"))
|
||||||
http.RouteRegister([]interface{}{
|
http.RouteRegister([]interface{}{
|
||||||
assetController.Asset,
|
assetController.Asset,
|
||||||
assetController.AssetSku,
|
assetController.AssetSku,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
"gitea.com/red-future/common/db/mongo"
|
"gitea.com/red-future/common/db/mongo"
|
||||||
"gitea.com/red-future/common/jaeger"
|
"gitea.com/red-future/common/jaeger"
|
||||||
"gitea.com/red-future/common/redis"
|
"gitea.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
@@ -36,7 +36,7 @@ func (s *capacity) UpdateLocationCapacity(ctx context.Context, locationId *bson.
|
|||||||
var zoneId *bson.ObjectID
|
var zoneId *bson.ObjectID
|
||||||
var capacityUnitType stock.CapacityUnitType
|
var capacityUnitType stock.CapacityUnitType
|
||||||
|
|
||||||
success, err := redis.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
success, err := utils.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
||||||
// 1. 查询库位信息
|
// 1. 查询库位信息
|
||||||
location, err := dao.Location.GetOne(ctx, &dto.GetLocationReq{Id: locationId})
|
location, err := dao.Location.GetOne(ctx, &dto.GetLocationReq{Id: locationId})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -179,7 +179,7 @@ func (s *capacity) SyncCapacityToZone(ctx context.Context, zoneId *bson.ObjectID
|
|||||||
lockKey := fmt.Sprintf("lock:zone:%s:capacity:%s", zoneId.Hex(), unitType)
|
lockKey := fmt.Sprintf("lock:zone:%s:capacity:%s", zoneId.Hex(), unitType)
|
||||||
expireSeconds := int64(30) // 30秒超时
|
expireSeconds := int64(30) // 30秒超时
|
||||||
|
|
||||||
success, err := redis.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
success, err := utils.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
||||||
// 2. 查询该库区下所有使用该单位类型的库位
|
// 2. 查询该库区下所有使用该单位类型的库位
|
||||||
locations, err := dao.Location.ListByZoneAndUnitType(ctx, zoneId, unitType)
|
locations, err := dao.Location.ListByZoneAndUnitType(ctx, zoneId, unitType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -242,7 +242,7 @@ func (s *capacity) SyncCapacityToWarehouse(ctx context.Context, warehouseId *bso
|
|||||||
lockKey := fmt.Sprintf("lock:warehouse:%s:capacity:%s", warehouseId.Hex(), unitType)
|
lockKey := fmt.Sprintf("lock:warehouse:%s:capacity:%s", warehouseId.Hex(), unitType)
|
||||||
expireSeconds := int64(30) // 30秒超时
|
expireSeconds := int64(30) // 30秒超时
|
||||||
|
|
||||||
success, err := redis.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
success, err := utils.Lock(ctx, lockKey, expireSeconds, func(ctx context.Context) error {
|
||||||
// 2. 查询该仓库下所有库区
|
// 2. 查询该仓库下所有库区
|
||||||
zones, err := dao.Zone.ListByWarehouseAndUnitType(ctx, warehouseId.Hex())
|
zones, err := dao.Zone.ListByWarehouseAndUnitType(ctx, warehouseId.Hex())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/redis"
|
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.com/red-future/common/utils"
|
||||||
gmq "github.com/bjang03/gmq/core/gmq"
|
gmq "github.com/bjang03/gmq/core/gmq"
|
||||||
"github.com/bjang03/gmq/mq"
|
"github.com/bjang03/gmq/mq"
|
||||||
@@ -188,7 +187,7 @@ func (s *stockManage) AddStock(ctx context.Context, msg any) error {
|
|||||||
ctx = context.WithValue(ctx, "tenantId", req.TenantId)
|
ctx = context.WithValue(ctx, "tenantId", req.TenantId)
|
||||||
// 获取redis-租户存储-锁key
|
// 获取redis-租户存储-锁key
|
||||||
fileLockKey := fmt.Sprintf(public.StockDetailLockKey, req.AssetSkuId)
|
fileLockKey := fmt.Sprintf(public.StockDetailLockKey, req.AssetSkuId)
|
||||||
success, err := redis.Lock(ctx, fileLockKey, int64(60), func(ctx context.Context) error {
|
success, err := utils.Lock(ctx, fileLockKey, int64(60), func(ctx context.Context) error {
|
||||||
if req.OperationType == "add" {
|
if req.OperationType == "add" {
|
||||||
if req.StockMode == stock.StockModeBatch {
|
if req.StockMode == stock.StockModeBatch {
|
||||||
if !g.IsEmpty(req.StockId) {
|
if !g.IsEmpty(req.StockId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user