The practical application of of PHP in queue programming

  • 2020-03-31 21:04:28
  • OfStack

A: the concept of queue, data structure

A Queue is a linear table whose operations are limited. A linear table that allows inserts at one end of the table and deletions at the other. The rear is the end that is allowed to be inserted. The front is the end that allows deletion. An empty queue is an empty table with no elements.

Suppose I have a queue Q= (a1, a2... , an), then a1 is the head element of the team and an is the tail element of the team. Elements are queued in order a1, a2... An, and the outgoing order is a1, a2... , an. You can see that the operation of the queue is conducted on a first-in, first-out basis.

< img Alt = "" border = 0 height = 122 SRC =" http://files.jb51.net/upload/201009/20100904214213507.jpg "width = 519 border = 0 >

For other details, please search the Internet for many materials.

Two: PHP queues

Queues are represented as arrays in PHP. The first element in the array is the head of the queue, and the last element is the tail, so the queue can be manipulated.

< img Alt = "" border = 0 height = 119 SRC =" http://files.jb51.net/upload/201009/20100904214213852.jpg "width = 411 border = 0 >

As a result,

< img Alt = "" border = 0 height = 73 SRC =" http://files.jb51.net/upload/201009/20100904214214213.jpg "width = 529 border = 0 >

There are a lot of packaged classes on the web that can be used directly.

Array_push: to push one or more cells into the end of an array (push)

Array_unshift: inserts one or more cells at the beginning of the array

Array_pop: pops the last cell of the array (out of stack)

Array_shift: moves the element at the beginning of the array out of the array

Three: Ruby Starling

Starling and Memcache use the same protocol but on different ports. Starling USES port 22122 and Memcache USES port 11211.

Ruby The tar XZVF ruby 1.9.1 - p0. Tar. Gz CD ruby 1.9.1 -- p0 . / configure -- prefix = / usr/local/huiyangruby The make Make install   The Gem The tar - ZXVF rubygems - 1.3.6. TGZ CD rubygems - 1.3.6 Ruby setup. Rb   Starling The gem install memcache - client starling starling Starling & // background execution Starling_top / / view PS information

Then you can use the queue to do your own thing. Starling and Memcache are used the same way, but they work better together.

< img Alt = "" border = 0 height = 162 SRC =" http://files.jb51.net/upload/201009/20100904214214879.jpg "width = 377 border = 0 >

Use Memcache: : addServer to create a Memcache connection pool. It is different from connect and pconnect in that it is connected only when there is a request, and no port connection.

Memcache: : connect - opens a connection to Memcache.

Memcache: : pconnect - opens a long connection to Memcache.

Memcache: : close - closes a Memcache connection.

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

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

Memcache: : replace - replaces an item that already exists on the Memcache server.

Memcache: : delete - deletes a saved item from the Memcache server.

Memcache: : flush - flushes all saved items on the Memcache server (similar to deleting all saved items).

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

Four: zhang yan works HTTPSQS

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

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

Five: queue application

Queues are great for asynchronous data transfer and storage, and when you are frequently inserting data into a database and submitting data to a search engine, you can use queues to do this asynchronously. In addition, slow processing logic and processing logic with concurrency limit can be placed in the background through message queue for processing, such as FLV video conversion, sending mobile phone SMS, sending E-mail, etc. (article by hou huiyang, phper.yang)


Related articles: