Practical application of queue in programming (php) _ PHP Tutorial

Source: Internet
Author: User
The practical application of queue in programming (php ). I. concept of Queue and data structure Queue are linear tables restricted by operation. Only insert at one end of the table is allowed, and delete Linear tables with elements at the other end. I. concept and data structure of a queue

A Queue is a linear table with limited operations. Only insert at one end of the table is allowed, and delete Linear tables with elements at the other end. A rear is the end that can be inserted. The frontend is the end that can be deleted. An empty queue is an empty table without any elements.

Suppose there is a queue Q = (a1, a2 ,..., An), then a1 is the head element, and an is the end element. Element teams are in the order of a1, a2 ,..., An, and the departure order is a1, a2 ,..., An. It can be seen that queue operations are performed in accordance with the first-in-first-out principle.

For more details, please search for a lot of information online.

II. PHP queue

In PHP, queues are represented in arrays. The first element in the array acts as the head of the team, and the last element acts as the end of the team, so that you can operate on this queue.

The result is

There are many encapsulated classes on the Internet that can be used directly.

Array_push: pushes one or more units to the end of the array (into the stack)

Array_unshift: insert one or more units at the beginning of the array

Array_pop: Bring up the last unit of the array (Out Stack)

Array_shift: removes elements starting with an array from the array.

III. Ruby Starling

Starling is a lightweight persistent server that supports the MemCache protocol. Starling makes it easy to create a network access queue or multiple queues, that is, asynchronous working processes between multiple nodes and multiple machines. It is developed by Twitter, a famous microblog website, to process a large number of queue messages and maintain service response. Starling is already used in the production environment, not only Twitter, but also FiveRuns. FiveRuns even made improvements based on their own applications.

Starling and Memcache use a protocol, but the port is different. Starling uses port 22122 and Memcache uses port 11211.

Rubytar xzvf ruby-1.9.1-p0.tar.gzcd ruby-1.9.1-p0. /configure -- prefix =/usr/local/huiyangrubymake make install Gemtar-zxvf rubygems-1.3.6.tgzcd rubygems-1.3.6ruby setup. rb Starlinggem install memcache-client starlingstarlingstarling & // execute starling_top in the background // View PS information

Next, you can use the queue to do your own thing. The usage of Starling and Memcache is the same, and the combination of Starling and Memcache is better.

You can use Memcache: addServer to create a memcache connection pool. He is different from connect and pconnect. he is connected only when there is a request, and no port is connected.

Memcache: connect -- open a connection to Memcache.

Memcache: pconnect -- open a persistent connection to Memcache.

Memcache: close -- close a Memcache connection.

Memcache: set -- save data to the Memcache server.

Memcache: get -- extract data stored on the Memcache server.

Memcache: replace -- replace a project that already exists on the Memcache server (similar to Memcache: set ).

Memcache: delete -- delete a saved project from the Memcache server.

Memcache: flush -- refresh the items saved on all Memcache servers (similar to deleting all saved items ).

Memcache: getStats -- get the running status of the current Memcache server.

IV. Banquet works HTTPSQS

HTTPSQS (HTTP Simple Queue Service) is a lightweight open-source Simple message Queue Service based on http get/POST protocol, use the B + Tree Key/Value database of Tokyo Cabinet for persistent data storage.

Interested can look at the Web site: http://blog.s135.com/httpsqs_1_2/

5. queue applications

The queue can process data transmission and storage asynchronously. when you insert data frequently into the database and submit data frequently to the search engine, you can adopt a queue for asynchronous insertion. In addition, you can put slow processing logic and limited number of concurrent processing logic in the background through message queues for processing, such as FLV video conversion, sending SMS messages, and sending email. (Text/Hou Huiyang PHPer. yang)

Queue is a linear table with limited operations. Only insert at one end of the table is allowed, and delete the linear table of elements at the other end ....

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.