<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace ModStart\Core\Util; use NinjaMutex\Lock\MySqlLock; use NinjaMutex\MutexFabric; class LockUtil { static $instance = null; private static function instance() { if (null === self::$instance) { goto UnK9U; kxcMI: $GmrG4 = new MutexFabric('mysql', $sX3cT); goto XRP2h; XRP2h: self::$instance = $GmrG4; goto zTcqi; UnK9U: $sX3cT = new MySqlLock(config('env.DB_USERNAME'), config('env.DB_PASSWORD'), config('env.DB_HOST')); goto kxcMI; zTcqi: } return self::$instance; } public static function acquire($sQIr6, $RPm2k = 60) { if (RedisUtil::isEnable()) { goto N2Kc7; yBaBK: return false; goto fmi5f; N2Kc7: $jPfeB = "Lock:{$sQIr6}"; goto KJnfk; BVItU: $Nleui = RedisUtil::get($jPfeB); goto R0nNS; KJnfk: if (RedisUtil::setnx($jPfeB, time() + $RPm2k)) { RedisUtil::expire($jPfeB, $RPm2k); return true; } goto BVItU; R0nNS: if ($Nleui < time()) { RedisUtil::delete($jPfeB); return self::acquire($sQIr6, $RPm2k); } goto yBaBK; fmi5f: } else { if (self::instance()->get($sQIr6)->acquireLock($RPm2k)) { return true; } } return false; } public static function release($sQIr6) { if (RedisUtil::isEnable()) { $jPfeB = "Lock:{$sQIr6}"; RedisUtil::delete($jPfeB); } else { self::instance()->get($sQIr6)->releaseLock(); } } }