PHP Queue Usage Example

  • 2021-07-24 10:37:52
  • OfStack

This article illustrates the use of PHP queues. Share it for your reference. The specific analysis is as follows:

What is a queue is a first-in-first-out linear table, which is usually realized by linked list or array in specific applications. Queue only allows insertion operation at the back end and deletion operation at the front end.

Under what circumstances will the queue be used? When concurrent requests want to ensure the integrity of transactions, the queue will be used. Of course, it is not excluded to use other better methods.

Queuing can also be used to relieve the pressure on the database server. We can put non-immediate data into the queue and execute it when the database is idle or after an interval of 1 period. For example, for access counters, there is no need to immediately execute the Sql that accesses the increase. When the queue is not used, the sql statement is like this, assuming that there are 5 people accessing:

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

With queues, you can do this:
update table1 set count=count+5 where id=1

Reduce the number of sql requests, thus achieving the effect of reducing the pressure on the server, of course, the number of visits is not very large websites are not necessary at all.
The following 1 queue class:

/**
* Queue
*
* @author jaclon
*
*/
class Queue
{
private $_queue = array();
protected $cache = null;
protected $queuecachename;
 
/**
* Construction method
* @param string $queuename Queue name
*/
function __construct($queuename)
{
 
$this->cache =& Cache::instance();
$this->queuecachename = 'queue_' . $queuename;
 
$result = $this->cache->get($this->queuecachename);
if (is_array($result)) {
$this->_queue = $result;
}
}
 
/**
* Will 1 Three cells are placed at the end of the queue
* @param mixed $value
*/
function enQueue($value)
{
$this->_queue[] = $value;
$this->cache->set($this->queuecachename, $this->_queue);
 
return $this;
}
 
/**
* Object at the beginning of the queue 1 One or more units are moved out
* @param int $num
*/
function sliceQueue($num = 1)
{
if (count($this->_queue) < $num) {
$num = count($this->_queue);
}
$output = array_splice($this->_queue, 0, $num);
$this->cache->set($this->queuecachename, $this->_queue);
 
return $output;
}
 
/**
* Move the cell at the beginning of the queue out of the queue
*/
function deQueue()
{
$entry = array_shift($this->_queue);
$this->cache->set($this->queuecachename, $this->_queue);
 
return $entry;
}
 
/**
* Returns queue length
*/
function size()
{
return count($this->_queue);
}
 
/**
* Returns the first in the queue 1 Unit
*/
function peek()
{
return $this->_queue[0];
}
 
/**
* Returns the 1 One or more units
* @param int $num
*/
function peeks($num)
{
if (count($this->_queue) < $num) {
$num = count($this->_queue);
}
return array_slice($this->_queue, 0, $num);
}
 
/**
* Destroy queue
*/
function destroy()
{
$this->cache->remove($this->queuecachename);
}
}

I hope this article is helpful to everyone's PHP programming.


Related articles: