refactor: 批量任务投递去重锁提炼BatchDispatchLock
This commit is contained in:
@@ -1608,8 +1608,15 @@ class PlatformOrderController extends Controller
|
|||||||
// 幂等/防抖(最小实现):避免运营短时间内重复点击导致重复投递同一批次。
|
// 幂等/防抖(最小实现):避免运营短时间内重复点击导致重复投递同一批次。
|
||||||
// 说明:这里做“短 TTL 的一次性锁”,不引入新表;后续可演进为批次表 + 幂等 key。
|
// 说明:这里做“短 TTL 的一次性锁”,不引入新表;后续可演进为批次表 + 幂等 key。
|
||||||
// key 口径:scope + filters + ids + limit(同一集合的重复点击会被拦截)。
|
// key 口径:scope + filters + ids + limit(同一集合的重复点击会被拦截)。
|
||||||
$lockKey = 'admin:bas:dispatch:' . md5($scope . '|' . $filterSummary . '|' . implode(',', $orderIds) . '|' . $limit);
|
if (! \App\Support\BatchDispatchLock::acquire(
|
||||||
if (! Cache::add($lockKey, '1', 60)) {
|
'admin:bas:dispatch',
|
||||||
|
$scope,
|
||||||
|
(string) $filterSummary,
|
||||||
|
$orderIds,
|
||||||
|
(int) $limit,
|
||||||
|
60,
|
||||||
|
'1',
|
||||||
|
)) {
|
||||||
return redirect()->back()->with('warning', '检测到刚刚已提交过同一批次的 BAS 任务(1 分钟内)。为避免重复投递,本次未再次提交。');
|
return redirect()->back()->with('warning', '检测到刚刚已提交过同一批次的 BAS 任务(1 分钟内)。为避免重复投递,本次未再次提交。');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1729,8 +1736,15 @@ class PlatformOrderController extends Controller
|
|||||||
// 幂等/防抖(最小实现):避免运营短时间内重复点击导致重复投递同一批次。
|
// 幂等/防抖(最小实现):避免运营短时间内重复点击导致重复投递同一批次。
|
||||||
// 说明:这里做“短 TTL 的一次性锁”,不引入新表;后续可演进为批次表 + 幂等 key。
|
// 说明:这里做“短 TTL 的一次性锁”,不引入新表;后续可演进为批次表 + 幂等 key。
|
||||||
// key 口径:scope + filters + ids + limit(同一集合的重复点击会被拦截)。
|
// key 口径:scope + filters + ids + limit(同一集合的重复点击会被拦截)。
|
||||||
$lockKey = 'admin:bmpa:dispatch:' . md5($scope . '|' . $filterSummary . '|' . implode(',', $orderIds) . '|' . $limit);
|
if (! \App\Support\BatchDispatchLock::acquire(
|
||||||
if (! Cache::add($lockKey, $runId, 60)) {
|
'admin:bmpa:dispatch',
|
||||||
|
$scope,
|
||||||
|
(string) $filterSummary,
|
||||||
|
$orderIds,
|
||||||
|
(int) $limit,
|
||||||
|
60,
|
||||||
|
(string) $runId,
|
||||||
|
)) {
|
||||||
return redirect()->back()->with('warning', '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内)。为避免重复投递,本次未再次提交。');
|
return redirect()->back()->with('warning', '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内)。为避免重复投递,本次未再次提交。');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
app/Support/BatchDispatchLock.php
Normal file
31
app/Support/BatchDispatchLock.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class BatchDispatchLock
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 最小幂等/防抖锁(短 TTL):避免运营短时间内重复点击导致重复投递同一批次。
|
||||||
|
*
|
||||||
|
* key 口径(与现有实现保持一致):scope + filters(summary) + ids + limit。
|
||||||
|
*
|
||||||
|
* @param int[] $orderIds
|
||||||
|
*/
|
||||||
|
public static function acquire(
|
||||||
|
string $namespace,
|
||||||
|
string $scope,
|
||||||
|
string $filterSummary,
|
||||||
|
array $orderIds,
|
||||||
|
int $limit,
|
||||||
|
int $ttlSeconds = 60,
|
||||||
|
string $value = '1',
|
||||||
|
): bool {
|
||||||
|
$orderIds = array_values(array_map('intval', $orderIds));
|
||||||
|
|
||||||
|
$lockKey = $namespace . ':' . md5($scope . '|' . $filterSummary . '|' . implode(',', $orderIds) . '|' . $limit);
|
||||||
|
|
||||||
|
return Cache::add($lockKey, $value, $ttlSeconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user