refactor: 批量任务投递去重锁提炼BatchDispatchLock
This commit is contained in:
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