日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

PHP 队列

發布時間:2023/11/27 生活经验 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP 队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是隊列,是先進先出的線性表,在具體應用中通常用鏈表或者數組來實現,隊列只允許在后端進行插入操作,在前端進行刪除操作。

什么情況下會用了隊列呢,并發請求又要保證事務的完整性的時候就會用到隊列,當然不排除使用其它更好的方法,知道的不仿說說看。

隊列還可以用于減輕數據庫服務器壓力,我們可以將不是即時數據放入到隊列中,在數據庫空閑的時候或者間隔一段時間后執行。比如訪問計數器,沒有必要即時的執行訪問增加的Sql,在沒有使用隊列的時候sql語句是這樣的,假設有5個人訪問:

  1. update table1 set count=count+1 where id=1?
  2. update table1 set count=count+1 where id=1?
  3. update table1 set count=count+1 where id=1?
  4. update table1 set count=count+1 where id=1?
  5. update table1 set count=count+1 where id=1?
update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1

而使用隊列這后就可以這樣:

  1. update table1 set count=count+5 where id=1?
update table1 set count=count+5 where id=1

減少sql請求次數,從而達到減輕服務器壓力的效果, 當然訪問量不是很大網站根本沒有這個必要。

下面一個隊列類:

  1. /**
  2. * 隊列
  3. *
  4. * @author jaclon
  5. *
  6. */?
  7. class Queue?
  8. {?
  9. private $_queue = array();?
  10. protected $cache = null;?
  11. protected $queuecachename;?
  12. ?
  13. /**
  14. * 構造方法
  15. * @param string $queuename 隊列名稱
  16. */?
  17. function __construct($queuename)?
  18. {?
  19. ?
  20. $this->cache =& Cache::instance();?
  21. $this->queuecachename = 'queue_' . $queuename;?
  22. ?
  23. $result = $this->cache->get($this->queuecachename);?
  24. if (is_array($result)) {?
  25. $this->_queue = $result;?
  26. }?
  27. }?
  28. ?
  29. /**
  30. * 將一個單元單元放入隊列末尾
  31. * @param mixed $value
  32. */?
  33. function enQueue($value)?
  34. {?
  35. $this->_queue[] = $value;?
  36. $this->cache->set($this->queuecachename, $this->_queue);?
  37. ?
  38. return $this;?
  39. }?
  40. ?
  41. /**
  42. * 將隊列開頭的一個或多個單元移出
  43. * @param int $num
  44. */?
  45. function sliceQueue($num = 1)?
  46. {?
  47. if (count($this->_queue) < $num) {?
  48. $num = count($this->_queue);?
  49. }?
  50. $output = array_splice($this->_queue, 0, $num);?
  51. $this->cache->set($this->queuecachename, $this->_queue);?
  52. ?
  53. return $output;?
  54. }?
  55. ?
  56. /**
  57. * 將隊列開頭的單元移出隊列
  58. */?
  59. function deQueue()?
  60. {?
  61. $entry = array_shift($this->_queue);?
  62. $this->cache->set($this->queuecachename, $this->_queue);?
  63. ?
  64. return $entry;?
  65. }?
  66. ?
  67. /**
  68. * 返回隊列長度
  69. */?
  70. function size()?
  71. {?
  72. return count($this->_queue);?
  73. }?
  74. ?
  75. /**
  76. * 返回隊列中的第一個單元
  77. */?
  78. function peek()?
  79. {?
  80. return $this->_queue[0];?
  81. }?
  82. ?
  83. /**
  84. * 返回隊列中的一個或多個單元
  85. * @param int $num
  86. */?
  87. function peeks($num)?
  88. {?
  89. if (count($this->_queue) < $num) {?
  90. $num = count($this->_queue);?
  91. }?
  92. return array_slice($this->_queue, 0, $num);?
  93. }?
  94. ?
  95. /**
  96. * 消毀隊列
  97. */?
  98. function destroy()?
  99. {?
  100. $this->cache->remove($this->queuecachename);?
  101. }?
  102. }?

轉自:http://blog.163.com/lgh_2002/blog/static/44017526201172511139202/

總結

以上是生活随笔為你收集整理的PHP 队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。