一:队列的概念、数据结构
队列(Queue)是运算受到限制的一种线性表。只允许在表的一端进行插入,而在另一端进行删除元素的线性表。队尾(rear)是允许插入的一端。队头(front)是允许删除的一端。空队列是不含元素的空表。
假设有个队列Q=(a1,a2,…,an),则a1为队头元素,an为队尾元素。元素入队的次序为a1,a2,…,an,而出队的次序为a1,a2,…,an。可见队列的操作是按照先进先出的原则进行的。
其他详细的介绍请在网上搜索很多资料。
二:PHP的队列
在PHP中队列以数组的形式表现。数组中的第一个元素作为队头,最后一个元素作为队尾,这样就可以操作这个队列了。
结果就是
网上有很多封装好的类,可以直接使用。
array_push:将一个或多个单元压入数组的末尾(入栈)
array_unshift:在数组开头插入一个或多个单元
array_pop:将数组最后一个单元弹出(出栈)
array_shift:将数组开头的单元移出数组
三:Ruby Starling
Starling是一个支持MemCache协议的轻量级持久化服务器。Starling是让创建网络访问队列或者多个队列异常简单,也就是说多点和多台机器间的异步工作进程。它是著名微博客网站Twitter开发用来处理大量的队列消息,以及保持服务的响应。Starling已经在生产环境中使用,不仅是Twitter在使用,FiveRuns同样在使用。FiveRuns甚至还根据自己的应用做了改进。
Starling和Memcache使用的是一个协议只是端口不一样。Starling使用的是22122端口,Memcache使用的是11211端口。
Ruby
tar xzvf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0
./configure --prefix=/usr/local/huiyangruby
make
make install
Gem
tar -zxvf rubygems-1.3.6.tgz
cd rubygems-1.3.6
ruby setup.rb
Starling
gem install memcache-client starling
starling
starling & //后台执行
starling_top //查看PS信息
|
Next you can use the queue to do your own things. Starling and Memcache are used in the same way, and it is better to work together.
Use Memcache::addServer to establish a memcache connection pool. It is different from connect and pconnect. It only connects when there is a request, otherwise the port connects.
Memcache::connect -- Open a connection to Memcache.
Memcache::pconnect -- Open a long connection to Memcache.
Memcache::close -- Close a Memcache connection.
Memcache::set -- Save data to the Memcache server.
Memcache::get -- Extract a data stored on the Memcache server.
Memcache::replace -- Replace an item that already exists on the Memcache server (function similar to Memcache::set).
Memcache::delete -- Delete 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 -- Get the current running status of the Memcache server.
Four: Zhang Yan’s 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 Tokyo Cabinet's B+Tree Key/Value database for persistent storage of data.
If you are interested, you can check out the website: http://blog.s135.com/httpsqs_1_2/
5: Queue Application
Queues can handle data transmission and storage asynchronously. When you frequently insert data into the database and frequently submit data to search engines, you can use queues for asynchronous insertion. In addition, slower processing logic and processing logic with limited concurrency can also be placed in the background for processing through message queues, such as FLV video conversion, sending mobile phone text messages, sending emails, etc. (Text/Hou Huiyang PHPer.yang)