bug修复
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
use app\exception\SysException;
|
||||
use support\Log;
|
||||
use support\Redis;
|
||||
use support\think\Cache;
|
||||
use Webman\RedisQueue\Client;
|
||||
|
||||
@@ -883,14 +884,10 @@ if(!function_exists('page')) {
|
||||
function runWithLock(string $key, int $expire, \Closure $callback)
|
||||
{
|
||||
$lockValue = uniqid();
|
||||
$val = cache($key);
|
||||
if ($val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 加锁(NX:不存在时设置,EX:过期时间)
|
||||
$acquired = cache($key, $lockValue, $expire);
|
||||
|
||||
// NX: 只在 key 不存在时设置;EX: 过期时间(秒)
|
||||
$acquired = Redis::setNx($key, $lockValue);
|
||||
Redis::setEx($key, $lockValue, $expire);
|
||||
if (!$acquired) {
|
||||
return false; // 获取锁失败
|
||||
}
|
||||
@@ -898,9 +895,14 @@ function runWithLock(string $key, int $expire, \Closure $callback)
|
||||
try {
|
||||
return $callback(); // 执行传入的方法
|
||||
} finally {
|
||||
release($key, $lockValue);
|
||||
// 释放锁:确保只删除自己加的锁
|
||||
$currentValue = cache($key);
|
||||
if ($currentValue === $lockValue) {
|
||||
cache()->forget($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('cache')) {
|
||||
/**
|
||||
* 缓存管理
|
||||
|
||||
Reference in New Issue
Block a user