This example describes the usage of PHP queues. Share to everyone for your reference. The specific analysis is as follows:
What is a queue, is a FIFO linear table, in the specific application of a linked list or array to implement, the queue only allows the back-end to insert operations, the front end of the delete operation.
Under what circumstances will the queue be used, concurrent requests to ensure the integrity of the transaction will be used when the queue, of course, do not rule out the use of other better methods, know not to say.
Queues can also be used to reduce the pressure on the database server, and we can put the data not in the queue, when the database is idle, or after a period of time. For example, access counters, there is no need to immediately execute access to the increased SQL, in the absence of queues when the SQL statement is this, assuming that there are 5 people access:
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
This can be done after the queue is used:
Update table1 set count=count+5 where id=1
Reduce the number of SQL requests, so as to reduce the effect of server pressure, of course, the number of visits is not very large site is not necessary.
One of the following queue classes:
Copy Code code as follows:
/**
* 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;
}
}
/**
* Put a cell unit at the end of the queue
* @param mixed $value
*/
function EnQueue ($value)
{
$this->_queue[] = $value;
$this->cache->set ($this->queuecachename, $this->_queue);
return $this;
}
/**
* Move one or more cells at the beginning of the queue 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 unit 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;
}
/**
* Return Queue Length
*/
function size ()
{
Return count ($this->_queue);
}
/**
* Returns the first cell in the queue
*/
function Peek ()
{
return $this->_queue[0];
}
/**
* Returns one or more units in the queue
* @param int $num
*/
function Peeks ($num)
{
if (count ($this->_queue) < $num) {
$num = count ($this->_queue);
}
Return Array_slice ($this->_queue, 0, $num);
}
/**
* Elimination of the destruction of the queue
*/
function Destroy ()
{
$this->cache->remove ($this->queuecachename);
}
}
I hope this article will help you with your PHP program design.