Home > php教程 > php手册 > body text

PHP 队列

WBOY
Release: 2016-06-06 19:54:36
Original
991 people have browsed it

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

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

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

队列还可以用于减轻数据库服务器压力,我们可以将不是即时数据放入到队列中,在数据库空闲的时候或者间隔一段时间后执行。比如访问计数器,没有必要即时的执行访问增加的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
Copy after login

而使用队列这后就可以这样:

  1. update table1 set count=count+5 where id=1 
update table1 set count=count+5 where id=1
Copy after login

减少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/

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!