> php教程 > php手册 > 본문

PHP 队列

WBOY
풀어 주다: 2016-06-06 19:54:36
원래의
991명이 탐색했습니다.

什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端进行插入操作,在前端进行删除操作。 什么情况下会用了队列呢,并发请求又要保证事务的完整性的时候就会用到队列,当然不排除使用其它更好的方法,知道的不仿说说看

什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端进行插入操作,在前端进行删除操作。

什么情况下会用了队列呢,并发请求又要保证事务的完整性的时候就会用到队列,当然不排除使用其它更好的方法,知道的不仿说说看。

队列还可以用于减轻数据库服务器压力,我们可以将不是即时数据放入到队列中,在数据库空闲的时候或者间隔一段时间后执行。比如访问计数器,没有必要即时的执行访问增加的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. private$_queue = array(); 
  9. protected$cache = null; 
  10. protected$queuecachename
  11.  
  12. /**
  13. * 构造方法
  14. * @param string $queuename 队列名称
  15. */ 
  16. function __construct($queuename
  17.  
  18. $this->cache =& Cache::instance(); 
  19. $this->queuecachename = 'queue_' . $queuename
  20.  
  21. $result = $this->cache->get($this->queuecachename); 
  22. if (is_array($result)) { 
  23. $this->_queue = $result
  24.  
  25. /**
  26. * 将一个单元单元放入队列末尾
  27. * @param mixed $value
  28. */ 
  29. function enQueue($value
  30. $this->_queue[] = $value
  31. $this->cache->set($this->queuecachename, $this->_queue); 
  32.  
  33. return$this
  34.  
  35. /**
  36. * 将队列开头的一个或多个单元移出
  37. * @param int $num
  38. */ 
  39. function sliceQueue($num = 1) 
  40. if (count($this->_queue) $num) { 
  41. $num = count($this->_queue); 
  42. $output = array_splice($this->_queue, 0, $num); 
  43. $this->cache->set($this->queuecachename, $this->_queue); 
  44.  
  45. return$output
  46.  
  47. /**
  48. * 将队列开头的单元移出队列
  49. */ 
  50. function deQueue() 
  51. $entry = array_shift($this->_queue); 
  52. $this->cache->set($this->queuecachename, $this->_queue); 
  53.  
  54. return$entry
  55.  
  56. /**
  57. * 返回队列长度
  58. */ 
  59. function size() 
  60. returncount($this->_queue); 
  61.  
  62. /**
  63. * 返回队列中的第一个单元
  64. */ 
  65. function peek() 
  66. return$this->_queue[0]; 
  67.  
  68. /**
  69. * 返回队列中的一个或多个单元
  70. * @param int $num
  71. */ 
  72. function peeks($num
  73. if (count($this->_queue) $num) { 
  74. $num = count($this->_queue); 
  75. returnarray_slice($this->_queue, 0, $num); 
  76.  
  77. /**
  78. * 消毁队列
  79. */ 
  80. function destroy() 
  81. $this->cache->remove($this->queuecachename); 

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!