国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現的memcache環形隊列類實例

2024-05-04 23:38:12
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP實現的memcache環形隊列類,實例分析了基于memcache實現環形隊列的方法,涉及memcache緩存及隊列的相關技巧,需要的朋友可以參考下

本文實例講述了PHP實現的memcache環形隊列類。分享給大家供大家參考。具體如下:

這里介紹了PHP實現的memcache環形隊列類。沒咋學過數據結構,因為業務需要,所以只是硬著頭皮模擬的! 參考PHP memcache 隊列代碼。為使隊列隨時可入可出,且不受int長度越界危險(單鏈采取Head自增的話不作處理有越界可能),所以索性改寫成環形隊列。可能還有BUG,忘見諒!

 

 
  1. <?php 
  2. /** 
  3. * PHP memcache 環形隊列類 
  4. * 原作者 LKK/lianq.net 
  5. * 修改 FoxHunter 
  6. * 因業務需要只保留的隊列中的Pop和Push,修改過期時間為0即永久 
  7. */ 
  8. class MQueue 
  9. public static $client
  10. private $expire//過期時間,秒,1~2592000,即30天內 
  11. private $sleepTime//等待解鎖時間,微秒 
  12. private $queueName//隊列名稱,唯一值 
  13. private $retryNum//嘗試次數 
  14. private $MAXNUM//最大隊列容量 
  15. private $canRewrite//是否可以覆寫開關,滿出來的內容從頭部開始覆蓋重寫原來的數據 
  16. private $HEAD//下一步要進入的指針位置 
  17. private $TAIL//下一步要進入的指針位置 
  18. private $LEN//隊列現有長度 
  19. const LOCK_KEY = '_Fox_MQ_LOCK_'//鎖存儲標示 
  20. const LENGTH_KEY = '_Fox_MQ_LENGTH_'//隊列現長度存儲標示 
  21. const VALU_KEY = '_Fox_MQ_VAL_'//隊列鍵值存儲標示 
  22. const HEAD_KEY = '_Fox_MQ_HEAD_'//隊列HEAD指針位置標示 
  23. const TAIL_KEY = '_Fox_MQ_TAIL_'//隊列TAIL指針位置標示 
  24. /* 
  25. * 構造函數 
  26. * 對于同一個$queueName,實例化時必須保障構造函數的參數值一致,否則pop和push會導隊列順序混亂 
  27. */ 
  28. public function __construct($queueName = ''$maxqueue = 1, $canRewrite = false, $expire = 0, $config = ''
  29. if (emptyempty($config)) { 
  30. self::$client = memcache_pconnect('127.0.0.1', 11211); 
  31. elseif (is_array($config)) { //array('host'=>'127.0.0.1','port'=>'11211') 
  32. self::$client = memcache_pconnect($config['host'], $config['port']); 
  33. elseif (is_string($config)) { //"127.0.0.1:11211" 
  34. $tmp = explode(':'$config); 
  35. $conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1'
  36. $conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211'
  37. self::$client = memcache_pconnect($conf['host'], $conf['port']); 
  38. if (!self::$client
  39. return false; 
  40. ignore_user_abort(true); //當客戶斷開連接,允許繼續執行 
  41. set_time_limit(0); //取消腳本執行延時上限 
  42. $this->access = false; 
  43. $this->sleepTime = 1000; 
  44. $expire = (emptyempty($expire)) ? 0 : (int) $expire + 1; 
  45. $this->expire = $expire
  46. $this->queueName = $queueName
  47. $this->retryNum = 20000; 
  48. $this->MAXNUM = $maxqueue != null ? $maxqueue : 1; 
  49. $this->canRewrite = $canRewrite
  50. $this->getHeadAndTail(); 
  51. if (!isset($this->HEAD) || emptyempty($this->HEAD)) 
  52. $this->HEAD = 0; 
  53. if (!isset($this->TAIL) || emptyempty($this->TAIL)) 
  54. $this->TAIL = 0; 
  55. if (!isset($this->LEN) || emptyempty($this->LEN)) 
  56. $this->LEN = 0; 
  57. //獲取隊列首尾指針信息和長度 
  58. private function getHeadAndTail() 
  59. $this->HEAD = (int) memcache_get(self::$client$this->queueName . self::HEAD_KEY); 
  60. $this->TAIL = (int) memcache_get(self::$client$this->queueName . self::TAIL_KEY); 
  61. $this->LEN = (int) memcache_get(self::$client$this->queueName . self::LENGTH_KEY); 
  62. // 利用memcache_add原子性加鎖 
  63. private function lock() 
  64. if ($this->access === false) { 
  65. $i = 0; 
  66. while (!memcache_add(self::$client$this->queueName . self::LOCK_KEY, 1, false, $this->expire)) { 
  67. usleep($this->sleepTime); 
  68. @$i++; 
  69. if ($i > $this->retryNum) { //嘗試等待N次 
  70. return false; 
  71. break
  72. return $this->access = true; 
  73. return false; 
  74. //更新頭部指針指向,指向下一個位置 
  75. private function incrHead() 
  76. //$this->getHeadAndTail(); //獲取最新指針信息 ,由于本方法體均在鎖內調用,其鎖內已調用了此方法,本行注釋 
  77. $this->HEAD++; //頭部指針下移 
  78. if ($this->HEAD >= $this->MAXNUM) { 
  79. $this->HEAD = 0; //邊界值修正 
  80. $this->LEN--; //Head的移動由Pop觸發,所以相當于數量減少 
  81. if ($this->LEN < 0) { 
  82. $this->LEN = 0; //邊界值修正 
  83. memcache_set(self::$client$this->queueName . self::HEAD_KEY, $this->HEAD, false, $this->expire); //更新 
  84. memcache_set(self::$client$this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 
  85. //更新尾部指針指向,指向下一個位置 
  86. private function incrTail() 
  87. //$this->getHeadAndTail(); //獲取最新指針信息,由于本方法體均在鎖內調用,其鎖內已調用了此方法,本行注釋 
  88. $this->TAIL++; //尾部指針下移 
  89. if ($this->TAIL >= $this->MAXNUM) { 
  90. $this->TAIL = 0; //邊界值修正 
  91. $this->LEN++; //Head的移動由Push觸發,所以相當于數量增加 
  92. if ($this->LEN >= $this->MAXNUM) { 
  93. $this->LEN = $this->MAXNUM; //邊界值長度修正 
  94. memcache_set(self::$client$this->queueName . self::TAIL_KEY, $this->TAIL, false, $this->expire); //更新 
  95. memcache_set(self::$client$this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 
  96. // 解鎖 
  97. private function unLock() 
  98. memcache_delete(self::$client$this->queueName . self::LOCK_KEY); 
  99. $this->access = false; 
  100. //判斷是否滿隊列 
  101. public function isFull() 
  102. //外部直接調用的時候由于沒有鎖所以此處的值是個大概值,并不很準確,但是內部調用由于在前面有lock,所以可信 
  103. if ($this->canRewrite) 
  104. return false; 
  105. return $this->LEN == $this->MAXNUM ? true : false; 
  106. //判斷是否為空 
  107. public function isEmpty() 
  108. //外部直接調用的時候由于沒有鎖所以此處的值是個大概值,并不很準確,但是內部調用由于在前面有lock,所以可信 
  109. return $this->LEN == 0 ? true : false; 
  110. public function getLen() 
  111. //外部直接調用的時候由于沒有鎖所以此處的值是個大概值,并不很準確,但是內部調用由于在前面有lock,所以可信 
  112. return $this->LEN; 
  113. /* 
  114. * push值 
  115. * @param mixed 值 
  116. * @return bool 
  117. */ 
  118. public function push($data = ''
  119. $result = false; 
  120. if (emptyempty($data)) 
  121. return $result
  122. if (!$this->lock()) { 
  123. return $result
  124. $this->getHeadAndTail(); //獲取最新指針信息 
  125. if ($this->isFull()) { //只有在非覆寫下才有Full概念 
  126. $this->unLock(); 
  127. return false; 
  128. if (memcache_set(self::$client$this->queueName . self::VALU_KEY . $this->TAIL, $data, MEMCACHE_COMPRESSED, $this->expire)) { 
  129. //當推送后,發現尾部和頭部重合(此時指針還未移動),且右邊仍有未由Head讀取的數據,那么移動Head指針,避免尾部指針跨越Head 
  130. if ($this->TAIL == $this->HEAD && $this->LEN >= 1) { 
  131. $this->incrHead(); 
  132. $this->incrTail(); //移動尾部指針 
  133. $result = true; 
  134. $this->unLock(); 
  135. return $result
  136. /* 
  137. * Pop一個值 
  138. * @param [length] int 隊列長度 
  139. * @return array 
  140. */ 
  141. public function pop($length = 0) 
  142. if (!is_numeric($length)) 
  143. return false; 
  144. if (!$this->lock()) 
  145. return false; 
  146. $this->getHeadAndTail(); 
  147. if (emptyempty($length)) 
  148. $length = $this->LEN; //默認讀取所有 
  149. if ($this->isEmpty()) { 
  150. $this->unLock(); 
  151. return false; 
  152. //獲取長度超出隊列長度后進行修正 
  153. if ($length > $this->LEN) 
  154. $length = $this->LEN; 
  155. $data = $this->popKeyArray($length); 
  156. $this->unLock(); 
  157. return $data
  158. /* 
  159. * pop某段長度的值 
  160. * @param [length] int 隊列長度 
  161. * @return array 
  162. */ 
  163. private function popKeyArray($length
  164. $result = array(); 
  165. if (emptyempty($length)) 
  166. return $result
  167. for ($k = 0; $k < $length$k++) { 
  168. $result[] = @memcache_get(self::$client$this->queueName . self::VALU_KEY . $this->HEAD); 
  169. @memcache_delete(self::$client$this->queueName . self::VALU_KEY . $this->HEAD, 0); 
  170. //當提取值后,發現頭部和尾部重合(此時指針還未移動),且右邊沒有數據,即隊列中最后一個數據被完全掏空,此時指針停留在本地不移動,隊列長度變為0 
  171. if ($this->TAIL == $this->HEAD && $this->LEN <= 1) { 
  172. $this->LEN = 0; 
  173. memcache_set(self::$client$this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新 
  174. break
  175. else { 
  176. $this->incrHead(); //首尾未重合,或者重合但是仍有未讀取出的數據,均移動HEAD指針到下一處待讀取位置 
  177. return $result
  178. /* 
  179. * 重置隊列 
  180. * * @return NULL 
  181. */ 
  182. private function reset($all = false) 
  183. if ($all) { 
  184. memcache_delete(self::$client$this->queueName . self::HEAD_KEY, 0); 
  185. memcache_delete(self::$client$this->queueName . self::TAIL_KEY, 0); 
  186. memcache_delete(self::$client$this->queueName . self::LENGTH_KEY, 0); 
  187. else { 
  188. $this->HEAD = $this->TAIL = $this->LEN = 0; 
  189. memcache_set(self::$client$this->queueName . self::HEAD_KEY, 0, false, $this->expire); 
  190. memcache_set(self::$client$this->queueName . self::TAIL_KEY, 0, false, $this->expire); 
  191. memcache_set(self::$client$this->queueName . self::LENGTH_KEY, 0, false, $this->expire); 
  192. /* 
  193. * 清除所有memcache緩存數據 
  194. * @return NULL 
  195. */ 
  196. public function memFlush() 
  197. memcache_flush(self::$client); 
  198. public function clear($all = false) 
  199. if (!$this->lock()) 
  200. return false; 
  201. $this->getHeadAndTail(); 
  202. $Head = $this->HEAD; 
  203. $Length = $this->LEN; 
  204. $curr = 0; 
  205. for ($i = 0; $i < $Length$i++) { 
  206. $curr = $this->$Head + $i
  207. if ($curr >= $this->MAXNUM) { 
  208. $this->HEAD = $curr = 0; 
  209. @memcache_delete(self::$client$this->queueName . self::VALU_KEY . $curr, 0); 
  210. $this->unLock(); 
  211. $this->reset($all); 
  212. return true; 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄龙县| 兴和县| 嘉义县| 茶陵县| 兴国县| 青川县| 锦屏县| 万盛区| 高陵县| 珠海市| 乌兰察布市| 大竹县| 莫力| 海林市| 鄂州市| 措勤县| 卓尼县| 吐鲁番市| 玛纳斯县| 云浮市| 焦作市| 金乡县| 崇左市| 蒙城县| 大港区| 吴川市| 湖北省| 阳城县| 田林县| 报价| 长泰县| 无棣县| 团风县| 南充市| 桑日县| 柞水县| 那曲县| 肥城市| 桂林市| 忻州市| 巴林右旗|