超市优化
This commit is contained in:
@ -30,6 +30,7 @@ import java.math.BigDecimal;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
import static cn.iocoder.yudao.module.member.service.amount.LockManager.getStoreOrderLock;
|
||||||
|
|
||||||
@Tag(name = "APP超市 - 商品订单")
|
@Tag(name = "APP超市 - 商品订单")
|
||||||
@RestController
|
@RestController
|
||||||
@ -66,13 +67,13 @@ public class AppStoreOrderController {
|
|||||||
|
|
||||||
@PostMapping("/appCreate")
|
@PostMapping("/appCreate")
|
||||||
@Operation(summary = "购物车订单")
|
@Operation(summary = "购物车订单")
|
||||||
public synchronized CommonResult<Boolean> appCreate(@RequestBody CardDto dto) {
|
public CommonResult<Boolean> appCreate(@RequestBody CardDto dto) {
|
||||||
|
|
||||||
|
|
||||||
AddReqVO addReqVO = storeOrderService.appCreate(dto);
|
|
||||||
//记录
|
|
||||||
memberAsyncService.batchRecord(addReqVO);
|
|
||||||
|
|
||||||
|
synchronized(getStoreOrderLock("create")){
|
||||||
|
AddReqVO addReqVO = storeOrderService.appCreate(dto);
|
||||||
|
//记录
|
||||||
|
memberAsyncService.batchRecord(addReqVO);
|
||||||
|
}
|
||||||
return success(true);
|
return success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,11 +98,11 @@ public class AppStoreOrderController {
|
|||||||
@GetMapping("/cancel")
|
@GetMapping("/cancel")
|
||||||
@Operation(summary = "取消订单")
|
@Operation(summary = "取消订单")
|
||||||
public synchronized CommonResult<Boolean> cancel(Integer orderId) {
|
public synchronized CommonResult<Boolean> cancel(Integer orderId) {
|
||||||
|
synchronized(getStoreOrderLock("cancel")){
|
||||||
AddReqVO cancel = storeOrderService.cancel(orderId);
|
AddReqVO cancel = storeOrderService.cancel(orderId);
|
||||||
//记录
|
//记录
|
||||||
memberAsyncService.batchRecord(cancel);
|
memberAsyncService.batchRecord(cancel);
|
||||||
|
}
|
||||||
return success(true);
|
return success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@ public class LockManager {
|
|||||||
|
|
||||||
private static final ConcurrentHashMap<Long, Object> supermarketLocks = new ConcurrentHashMap<>();
|
private static final ConcurrentHashMap<Long, Object> supermarketLocks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private static final ConcurrentHashMap<String, Object> storeOrderLock = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public static Object getUserLock(Long userId) {
|
public static Object getUserLock(Long userId) {
|
||||||
// 从 Map 中获取 WeakReference 对象
|
// 从 Map 中获取 WeakReference 对象
|
||||||
return userLocks.computeIfAbsent(userId, k -> new Object());
|
return userLocks.computeIfAbsent(userId, k -> new Object());
|
||||||
@ -24,5 +26,10 @@ public class LockManager {
|
|||||||
// 对每个 userId 生成独立的锁对象,并发地存储在 ConcurrentHashMap 中
|
// 对每个 userId 生成独立的锁对象,并发地存储在 ConcurrentHashMap 中
|
||||||
return supermarketLocks.computeIfAbsent(storeId, k -> new Object());
|
return supermarketLocks.computeIfAbsent(storeId, k -> new Object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object getStoreOrderLock(String key) {
|
||||||
|
// 对每个 userId 生成独立的锁对象,并发地存储在 ConcurrentHashMap 中
|
||||||
|
return storeOrderLock.computeIfAbsent(key, k -> new Object());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user