PHP Queue usage instance, PHP queue Instance _php tutorial

Source: Internet
Author: User

PHP Queue usage instance, PHP queue instance


The example in this article describes the PHP queue usage. Share to everyone for your reference. The specific analysis is as follows:

What is a queue, is a FIFO linear table, usually in a specific application with a linked list or array to implement, the queue is only allowed to insert operations on the backend, the front-end to delete operations.

Under what circumstances will use the queue, concurrent requests to ensure the integrity of the transaction when the queue will be used, of course, do not rule out the use of other better methods, know not to imitate to say look.

Queues can also be used to relieve the pressure on the database server, and we can put non-immediate data into the queue and execute it 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, when the queue is not used when the SQL statement is like 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 using the queue:
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 amount of traffic is not very large site is not necessary.
The following queue class:
Copy CodeThe code is 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 unit 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 a queue
* @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 cells 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 a queue
*/
function Peek ()
{
return $this->_queue[0];
}

/**
* Returns one or more cells 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);
}

/**
* Destroy queue
*/
function Destroy ()
{
$this->cache->remove ($this->queuecachename);
}
}

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


What is the stack is the queue, try to give two examples of application

Stack is the first input data after the output;
Queue is the first input data first output;
Does that sound like a good idea? The example is difficult to say, very long, our teacher said two lessons, on the stack and the queue
Hope to help you.

Summer

Implementing a two-way queue with PHP

The problem is unclear

http://www.bkjia.com/PHPjc/906670.html www.bkjia.com true http://www.bkjia.com/PHPjc/906670.html techarticle PHP Queue Usage instance, PHP Queue instance This article describes the PHP queue usage. Share to everyone for your reference. The specific analysis is as follows: What is a queue, is a FIFO linear table, ...

  • 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.