PHP queue usage instance

Source: Internet
Author: User
This article mainly introduces the usage of PHP queues and describes the implementation and various common operations of PHP queues in the form of instances, which is of reference value, for more information about the queue usage in PHP, see the following example. Share it with you for your reference. The specific analysis is as follows:

What is a queue? it is a first-in-first-out linear table. in a specific application, linked lists or arrays are commonly used. a queue can only be inserted at the backend and deleted at the front end.

Under what circumstances will a queue be used? when concurrent requests are required to ensure transaction integrity, the queue will be used. of course, other better methods are not ruled out. I don't know what to say.

The queue can also be used to reduce the pressure on the database server. we can put non-instant data into the queue and execute it when the database is idle or after a period of time. For example, the access counter does not need to execute the SQL statement added for access in real time. the SQL statement is like this when the queue is not used. suppose there are five users who access the database:

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

The queue can be used as follows:
Update table1 set count = count + 5 where id = 1

Reduce the number of SQL requests to reduce the pressure on the server. of course, this is not necessary for websites with low access traffic.
The following queue class:

The code is as follows:

/**
* Queue
*
* @ Author jaclon
*
*/
Class Queue
{
Private $ _ queue = array ();
Protected $ cache = null;
Protected $ queuecachename;

/**
* Constructor
* @ 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 at the end of the queue
* @ Param mixed $ value
*/
Function enQueue ($ value)
{
$ This-> _ queue [] = $ value;
$ This-> cache-> set ($ this-> queuecachename, $ this-> _ queue );

Return $ this;
}

/**
* Remove one or more units starting with the 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;
}

/**
* Remove the unit starting with the queue from 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 unit in the queue.
*/
Function peek ()
{
Return $ this-> _ queue [0];
}

/**
* Return 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 );
}

/**
* Destroy a queue
*/
Function destroy ()
{
$ This-> cache-> remove ($ this-> queuecachename );
}
}

I hope this article will help you with PHP programming.

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.