PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類(lèi)實(shí)例
本文實(shí)例講述了PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類(lèi)。分享給大家供大家參考。具體如下:
這里介紹了PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類(lèi)。沒(méi)咋學(xué)過(guò)數(shù)據(jù)結(jié)構(gòu),因?yàn)闃I(yè)務(wù)需要,所以只是硬著頭皮模擬的! 參考PHP memcache 隊(duì)列代碼。為使隊(duì)列隨時(shí)可入可出,且不受int長(zhǎng)度越界危險(xiǎn)(單鏈采取Head自增的話不作處理有越界可能),所以索性改寫(xiě)成環(huán)形隊(duì)列??赡苓€有BUG,忘見(jiàn)諒!
<?php /** * PHP memcache 環(huán)形隊(duì)列類(lèi) * 原作者 LKK/lianq.net * 修改 FoxHunter * 因業(yè)務(wù)需要只保留的隊(duì)列中的Pop和Push,修改過(guò)期時(shí)間為0即永久 */ class MQueue { public static $client; private $expire; //過(guò)期時(shí)間,秒,1~2592000,即30天內(nèi) private $sleepTime; //等待解鎖時(shí)間,微秒 private $queueName; //隊(duì)列名稱(chēng),唯一值 private $retryNum; //嘗試次數(shù) private $MAXNUM; //最大隊(duì)列容量 private $canRewrite; //是否可以覆寫(xiě)開(kāi)關(guān),滿出來(lái)的內(nèi)容從頭部開(kāi)始覆蓋重寫(xiě)原來(lái)的數(shù)據(jù) private $HEAD; //下一步要進(jìn)入的指針位置 private $TAIL; //下一步要進(jìn)入的指針位置 private $LEN; //隊(duì)列現(xiàn)有長(zhǎng)度 const LOCK_KEY = '_Fox_MQ_LOCK_'; //鎖存儲(chǔ)標(biāo)示 const LENGTH_KEY = '_Fox_MQ_LENGTH_'; //隊(duì)列現(xiàn)長(zhǎng)度存儲(chǔ)標(biāo)示 const VALU_KEY = '_Fox_MQ_VAL_'; //隊(duì)列鍵值存儲(chǔ)標(biāo)示 const HEAD_KEY = '_Fox_MQ_HEAD_'; //隊(duì)列HEAD指針位置標(biāo)示 const TAIL_KEY = '_Fox_MQ_TAIL_'; //隊(duì)列TAIL指針位置標(biāo)示 /* * 構(gòu)造函數(shù) * 對(duì)于同一個(gè)$queueName,實(shí)例化時(shí)必須保障構(gòu)造函數(shù)的參數(shù)值一致,否則pop和push會(huì)導(dǎo)隊(duì)列順序混亂 */ public function __construct($queueName = '', $maxqueue = 1, $canRewrite = false, $expire = 0, $config = '') { if (empty($config)) { self::$client = memcache_pconnect('127.0.0.1', 11211); } elseif (is_array($config)) { //array('host'=>'127.0.0.1','port'=>'11211') self::$client = memcache_pconnect($config['host'], $config['port']); } elseif (is_string($config)) { //"127.0.0.1:11211" $tmp = explode(':', $config); $conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1'; $conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211'; self::$client = memcache_pconnect($conf['host'], $conf['port']); } if (!self::$client) return false; ignore_user_abort(true); //當(dāng)客戶斷開(kāi)連接,允許繼續(xù)執(zhí)行 set_time_limit(0); //取消腳本執(zhí)行延時(shí)上限 $this->access = false; $this->sleepTime = 1000; $expire = (empty($expire)) ? 0 : (int) $expire + 1; $this->expire = $expire; $this->queueName = $queueName; $this->retryNum = 20000; $this->MAXNUM = $maxqueue != null ? $maxqueue : 1; $this->canRewrite = $canRewrite; $this->getHeadAndTail(); if (!isset($this->HEAD) || empty($this->HEAD)) $this->HEAD = 0; if (!isset($this->TAIL) || empty($this->TAIL)) $this->TAIL = 0; if (!isset($this->LEN) || empty($this->LEN)) $this->LEN = 0; } //獲取隊(duì)列首尾指針信息和長(zhǎng)度 private function getHeadAndTail() { $this->HEAD = (int) memcache_get(self::$client, $this->queueName . self::HEAD_KEY); $this->TAIL = (int) memcache_get(self::$client, $this->queueName . self::TAIL_KEY); $this->LEN = (int) memcache_get(self::$client, $this->queueName . self::LENGTH_KEY); } // 利用memcache_add原子性加鎖 private function lock() { if ($this->access === false) { $i = 0; while (!memcache_add(self::$client, $this->queueName . self::LOCK_KEY, 1, false, $this->expire)) { usleep($this->sleepTime); @$i++; if ($i > $this->retryNum) { //嘗試等待N次 return false; break; } } return $this->access = true; } return false; } //更新頭部指針指向,指向下一個(gè)位置 private function incrHead() { //$this->getHeadAndTail(); //獲取最新指針信息 ,由于本方法體均在鎖內(nèi)調(diào)用,其鎖內(nèi)已調(diào)用了此方法,本行注釋 $this->HEAD++; //頭部指針下移 if ($this->HEAD >= $this->MAXNUM) { $this->HEAD = 0; //邊界值修正 } ; $this->LEN--; //Head的移動(dòng)由Pop觸發(fā),所以相當(dāng)于數(shù)量減少 if ($this->LEN < 0) { $this->LEN = 0; //邊界值修正 } ; memcache_set(self::$client, $this->queueName . self::HEAD_KEY, $this->HEAD, false, $this->expire); //更新 memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 } //更新尾部指針指向,指向下一個(gè)位置 private function incrTail() { //$this->getHeadAndTail(); //獲取最新指針信息,由于本方法體均在鎖內(nèi)調(diào)用,其鎖內(nèi)已調(diào)用了此方法,本行注釋 $this->TAIL++; //尾部指針下移 if ($this->TAIL >= $this->MAXNUM) { $this->TAIL = 0; //邊界值修正 } ; $this->LEN++; //Head的移動(dòng)由Push觸發(fā),所以相當(dāng)于數(shù)量增加 if ($this->LEN >= $this->MAXNUM) { $this->LEN = $this->MAXNUM; //邊界值長(zhǎng)度修正 } ; memcache_set(self::$client, $this->queueName . self::TAIL_KEY, $this->TAIL, false, $this->expire); //更新 memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 } // 解鎖 private function unLock() { memcache_delete(self::$client, $this->queueName . self::LOCK_KEY); $this->access = false; } //判斷是否滿隊(duì)列 public function isFull() { //外部直接調(diào)用的時(shí)候由于沒(méi)有鎖所以此處的值是個(gè)大概值,并不很準(zhǔn)確,但是內(nèi)部調(diào)用由于在前面有l(wèi)ock,所以可信 if ($this->canRewrite) return false; return $this->LEN == $this->MAXNUM ? true : false; } //判斷是否為空 public function isEmpty() { //外部直接調(diào)用的時(shí)候由于沒(méi)有鎖所以此處的值是個(gè)大概值,并不很準(zhǔn)確,但是內(nèi)部調(diào)用由于在前面有l(wèi)ock,所以可信 return $this->LEN == 0 ? true : false; } public function getLen() { //外部直接調(diào)用的時(shí)候由于沒(méi)有鎖所以此處的值是個(gè)大概值,并不很準(zhǔn)確,但是內(nèi)部調(diào)用由于在前面有l(wèi)ock,所以可信 return $this->LEN; } /* * push值 * @param mixed 值 * @return bool */ public function push($data = '') { $result = false; if (empty($data)) return $result; if (!$this->lock()) { return $result; } $this->getHeadAndTail(); //獲取最新指針信息 if ($this->isFull()) { //只有在非覆寫(xiě)下才有Full概念 $this->unLock(); return false; } if (memcache_set(self::$client, $this->queueName . self::VALU_KEY . $this->TAIL, $data, MEMCACHE_COMPRESSED, $this->expire)) { //當(dāng)推送后,發(fā)現(xiàn)尾部和頭部重合(此時(shí)指針還未移動(dòng)),且右邊仍有未由Head讀取的數(shù)據(jù),那么移動(dòng)Head指針,避免尾部指針跨越Head if ($this->TAIL == $this->HEAD && $this->LEN >= 1) { $this->incrHead(); } $this->incrTail(); //移動(dòng)尾部指針 $result = true; } $this->unLock(); return $result; } /* * Pop一個(gè)值 * @param [length] int 隊(duì)列長(zhǎng)度 * @return array */ public function pop($length = 0) { if (!is_numeric($length)) return false; if (!$this->lock()) return false; $this->getHeadAndTail(); if (empty($length)) $length = $this->LEN; //默認(rèn)讀取所有 if ($this->isEmpty()) { $this->unLock(); return false; } //獲取長(zhǎng)度超出隊(duì)列長(zhǎng)度后進(jìn)行修正 if ($length > $this->LEN) $length = $this->LEN; $data = $this->popKeyArray($length); $this->unLock(); return $data; } /* * pop某段長(zhǎng)度的值 * @param [length] int 隊(duì)列長(zhǎng)度 * @return array */ private function popKeyArray($length) { $result = array(); if (empty($length)) return $result; for ($k = 0; $k < $length; $k++) { $result[] = @memcache_get(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD); @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD, 0); //當(dāng)提取值后,發(fā)現(xiàn)頭部和尾部重合(此時(shí)指針還未移動(dòng)),且右邊沒(méi)有數(shù)據(jù),即隊(duì)列中最后一個(gè)數(shù)據(jù)被完全掏空,此時(shí)指針停留在本地不移動(dòng),隊(duì)列長(zhǎng)度變?yōu)? if ($this->TAIL == $this->HEAD && $this->LEN <= 1) { $this->LEN = 0; memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 break; } else { $this->incrHead(); //首尾未重合,或者重合但是仍有未讀取出的數(shù)據(jù),均移動(dòng)HEAD指針到下一處待讀取位置 } } return $result; } /* * 重置隊(duì)列 * * @return NULL */ private function reset($all = false) { if ($all) { memcache_delete(self::$client, $this->queueName . self::HEAD_KEY, 0); memcache_delete(self::$client, $this->queueName . self::TAIL_KEY, 0); memcache_delete(self::$client, $this->queueName . self::LENGTH_KEY, 0); } else { $this->HEAD = $this->TAIL = $this->LEN = 0; memcache_set(self::$client, $this->queueName . self::HEAD_KEY, 0, false, $this->expire); memcache_set(self::$client, $this->queueName . self::TAIL_KEY, 0, false, $this->expire); memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, 0, false, $this->expire); } } /* * 清除所有memcache緩存數(shù)據(jù) * @return NULL */ public function memFlush() { memcache_flush(self::$client); } public function clear($all = false) { if (!$this->lock()) return false; $this->getHeadAndTail(); $Head = $this->HEAD; $Length = $this->LEN; $curr = 0; for ($i = 0; $i < $Length; $i++) { $curr = $this->$Head + $i; if ($curr >= $this->MAXNUM) { $this->HEAD = $curr = 0; } @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $curr, 0); } $this->unLock(); $this->reset($all); return true; } }
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- 約瑟夫環(huán)問(wèn)題的PHP實(shí)現(xiàn) 使用PHP數(shù)組內(nèi)部指針操作函數(shù)
- PHP使用棧解決約瑟夫環(huán)問(wèn)題算法示例
- PHP實(shí)現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問(wèn)題示例
- PHP基于遞歸實(shí)現(xiàn)的約瑟夫環(huán)算法示例
- php解決約瑟夫環(huán)示例
- PHP環(huán)形鏈表實(shí)現(xiàn)方法示例
- PHP基于回溯算法解決n皇后問(wèn)題的方法示例
- php實(shí)現(xiàn)猴子選大王問(wèn)題算法實(shí)例
- PHP貪婪算法解決0-1背包問(wèn)題實(shí)例分析
- php基于環(huán)形鏈表解決約瑟夫環(huán)問(wèn)題示例
相關(guān)文章
PHP使用Redis實(shí)現(xiàn)防止大并發(fā)下二次寫(xiě)入的方法
這篇文章主要介紹了PHP使用Redis實(shí)現(xiàn)防止大并發(fā)下二次寫(xiě)入的方法,結(jié)合實(shí)例形式分析了php使用鎖機(jī)制實(shí)現(xiàn)并發(fā)讀寫(xiě)redis情況下的讀寫(xiě)錯(cuò)誤,需要的朋友可以參考下2017-10-10PHP實(shí)現(xiàn)仿Google分頁(yè)效果的分頁(yè)函數(shù)
這篇文章主要介紹了PHP實(shí)現(xiàn)仿Google分頁(yè)效果的分頁(yè)函數(shù),實(shí)例分析了php實(shí)現(xiàn)分頁(yè)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Ubuntu下安裝PHP的mongodb擴(kuò)展操作命令
這篇文章主要介紹了Ubuntu下安裝PHP的mongodb擴(kuò)展操作命令,本文給出下載址以及操作命令,本文使用編譯安裝方式,需要的朋友可以參考下2015-07-07