bug修复

This commit is contained in:
张松
2025-08-20 13:35:19 +08:00
parent 10f1667428
commit 0c27a542bb

View File

@@ -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')) {
/**
* 缓存管理