From 0c27a542bb579b735400a57aa4ba02c55927df8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Wed, 20 Aug 2025 13:35:19 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/functions.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/functions.php b/app/functions.php index 74e7f24..c814f6c 100644 --- a/app/functions.php +++ b/app/functions.php @@ -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')) { /** * 缓存管理