PHP Queue usage Example _php tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.