getUniqueLockId(); $result = Redis::set($lock_str, $unique_id, 'ex', $expire_time, 'nx'); return $result ? $unique_id : false; } catch (\Exception $e) { Log::channel('genera_error')->error("Error while acquiring lock for {$lock_str}: " . $e->getMessage()); return false; } } /** * 解锁 * @param string $lock_str 锁字符串 * @param string $unique_id 唯一锁ID * @return bool */ public function unlock(string $lock_str, string $unique_id): bool { try { Redis::watch($lock_str); if (Redis::exists($lock_str) && $unique_id == Redis::get($lock_str)) { Redis::multi()->del($lock_str)->exec(); return true; } Redis::unwatch(); return false; } catch (\Exception $e) { Log::channel('genera_error')->error("Error while releasing lock for {$lock_str}: " . $e->getMessage()); return false; } } /** * 获取唯一锁ID * @return string */ protected function getUniqueLockId(): string { return Str::uuid()->toString(); } }