<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ``` ~~~ <?php namespace app\common\service; use think\Exception; class RedisLock { /** * 單據鎖redis key模板 */ const REDIS_LOCK_KEY_TEMPLATE = 'order_lock_%s'; /** * 單據鎖默認超時時間(秒) */ const REDIS_LOCK_DEFAULT_EXPIRE_TIME = 8; /** * Redis配置:IP */ const REDIS_CONFIG_HOST = '127.0.0.1'; /** * Redis配置:端口 */ const REDIS_CONFIG_PORT = 6379; /** * 加鎖key */ const REDIS_LOCK_UNIQUE_ID_KEY = 'lock_unique_id'; /** * @param $intOrderId * @param int $intExpireTime * @return bool|int * @throws Exception * @describe: redis 加鎖 */ public static function lock($intOrderId, $intExpireTime = self::REDIS_LOCK_DEFAULT_EXPIRE_TIME) { //參數校驗 if (empty($intOrderId) || $intExpireTime <= 0) { return false; } //獲取Redis連接 $objRedisConn = self::getRedisConn(); //生成唯一鎖ID,解鎖需持有此ID $intUniqueLockId = self::generateUniqueLockId(); //根據板,結合單據ID,生成唯一Redis key(一般來說,單據ID在業務中系統中唯一的) $strKey = sprintf(self::REDIS_LOCK_KEY_TEMPLATE, $intOrderId); //加鎖(通過Redis setnx指令實現,從Redis 2.6.12開始,通過set指令可選參數也可以實現setnx,同時可原子化地設置超時時間) $bolRes = $objRedisConn->set($strKey, $intUniqueLockId, ['nx', 'ex'=>$intExpireTime]); //加鎖成功返回鎖ID,加鎖失敗返回false return $bolRes ? $intUniqueLockId : $bolRes; } /** * @param $intOrderId * @param $intLockId * @return bool * @throws Exception * @describe:redis 解鎖 */ public static function unlock($intOrderId, $intLockId) { //參數校驗 if (empty($intOrderId) || empty($intLockId)) { return false; } //獲取Redis連接 $objRedisConn = self::getRedisConn(); //生成Redis key $strKey = sprintf(self::REDIS_LOCK_KEY_TEMPLATE, $intOrderId); //監聽Redis key防止在【比對lock id】與【解鎖事務執行過程中】被修改或刪除,提交事務后會自動取消監控,其他情況需手動解除監控 $objRedisConn->watch($strKey); if ($intLockId == $objRedisConn->get($strKey)) { $objRedisConn->multi()->del($strKey)->exec(); return true; } $objRedisConn->unwatch(); return false; } /** * @param string $strIp * @param int $intPort * @return \Redis * @throws Exception * @describe:獲取Redis連接(簡易版本,可用單例實現) */ public static function getRedisConn($strIp = self::REDIS_CONFIG_HOST, $intPort = self::REDIS_CONFIG_PORT) { try { if (!extension_loaded('redis')) { throw new \BadFunctionCallException('not support: redis'); } $objRedis = new \Redis(); $objRedis->connect($strIp, $intPort); return $objRedis; }catch (Exception $exception){ throw new Exception($exception->getMessage()); } } /** * @return int * @throws Exception * @describe:生成鎖唯一ID(通過Redis incr指令實現簡易版本,可結合日期、時間戳、取余、字符串填充、隨機數等函數,生成指定位數唯一ID) */ public static function generateUniqueLockId() { return self::getRedisConn()->incr(self::REDIS_LOCK_UNIQUE_ID_KEY); } } ~~~ ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看