<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                百度百科上的解釋:大多是基于數據版本( Version )記錄機制實現。何謂數據版本?即為數據增加一個版本標識,在基于數據庫表的版本解決方案中,一般是通過為數據庫表增加一個 “version” 字段來實現。讀取出數據時,將此版本號一同讀出,之后更新時,對此版本號加一。此時,將提交數據的版本數據與數據庫表對應記錄的當前版本信息進行比對,如果提交的數據版本號大于數據庫表當前版本號,則予以更新,否則認為是過期數據。 **悲觀鎖比較適合強一致性的場景,但效率比較低,特別是讀的并發低。樂觀鎖則適用于讀多寫少,并發沖突少的場景。** ```php <?php /** * redis實戰 * * 實現悲觀鎖機制 * * @author TIGERB <https://github.com/TIGERB> * @example php pessmistic-lock.php */ $timeout = 5000; $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); do { $microtime = microtime(true) * 1000; $microtimeout = $microtime+$timeout+1; // 上鎖 $isLock = $redis->setnx('lock.count', $microtimeout); if (!$isLock) { $getTime = $redis->get('lock.count'); if ($getTime > $microtime) { // 睡眠 降低搶鎖頻率 緩解redis壓力 usleep(5000); // 未超時繼續等待 continue; } // 超時,搶鎖,可能有幾毫秒級時間差可忽略 $previousTime = $redis->getset('lock.count', $microtimeout); if ((int)$previousTime < $microtime) { break; } } } while (!$isLock); $count = $redis->get('count')? : 0; // file_put_contents('/var/log/count.log.1', ($count+1)); // 業務邏輯 echo "執行count加1操作~ \n\n"; $redis->set('count', $count+1); // 刪除鎖 $redis->del('lock.count'); // 打印count值 $count = $redis->get('count'); echo "count值為:$count \n"; ``` **另外的方法** ``` function lock($strMutex, $intTimeout) { $objRedis = new Redis(); //使用setnx原子型操作加鎖 $intRet = $objRedis->setnx($strMutex, 1); if ($intRet) { //設置過期時間,防止死任務的出現 $objRedis->expire($strMutex, $intTimeout); return true; } return false; } ``` ``` function lock($strMutex, $intTimeout, $intMaxTimes = 0) { $objRedis = new Redis(); //使用incr原子型操作加鎖 $intRet = $objRedis->incr($strMutex); if ($intRet === 1) { //設置過期時間,防止死任務的出現 $objRedis->expire($strMutex, $intTimeout); return true; } if ($intMaxTimes > 0 && $intRet >= $intMaxTimes && $objRedis->ttl($strMutex) === -1) { //當設置了最大加鎖次數時,如果嘗試加鎖次數大于最大加鎖次數并且無過期時間則強制解鎖 $objRedis->del($strMutex); } return false; } ``` ``` function lock($strMutex, $intTimeout) { $objRedis = new Redis(); //使用setnx操作加鎖,同時設置過期時間 $strRet = $objRedis->set($strMutex, 1, 'ex', $intTimeout, 'nx'); if ($strRet === 'OK') { return true; } return false; } ```
                  <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>

                              哎呀哎呀视频在线观看