Memcacheq of the "Go" persistence message queue

Source: Internet
Author: User
Tags message queue zend zend framework

G Memcacheq as MESSAGE QUEUE

PHP, Message Queuing, Memcacheq

Using Message Queuing, you can postpone some time-consuming work and then slowly execute it in the background,
This will not allow your users to wait too long.

Today, we introduce the message queue for PHP: Memcacheq.

Memcacheq

Features of the Memcacheq:
1 Easy to use
2 Faster processing speed
More than 3 queues
4 Concurrency performance is good
5 compatible with the Memcache protocol. This means that as long as the memcache extension is available, no additional plugins are required.
6 It is also very convenient to use in the Zend Framework.

Memcacheq relies on libevent and berkleydb.
Berkleydb data that is used to persist the storage queue. So when the Memcacheq crashes or the server hangs up,
Data loss is not a result. This is important and important.

Memcacheq would run as a daemon and communicates over the UDP protocol. It can handle concurrent connections and can is used in a load-balanced environment.

Startup parameters:

memcacheq-d-r-h/data/memcacheq-n-r-v-l 1024-u nobody &> \
/data/memcacheq/error.log
Explain:
-D Daemon
-R Maximize Core file limit
-V Verbose output
-U runs as a user
Save directory for-H BDB file
-N Performance Improvement
-R too long log file will be deleted.
-l log cache size, default is 32K. 1024 means 1024K.

Other parameters:
-H Help
-VV more detailed output
If you do not use-D, the output is displayed directly to the console.

Zend_queue

The Zend Framework has a adapter that communicates with Memcacheq:
Zend_queue_adapter_memcacheq

Http://framework.zend.com/manual/en/zend.queue.adapters.html

The following is a real-world example of how to use Memcacheq in the Zend Framework.

A news site, every news display, it should show the volume of this news visit, but also to add 1 of its traffic.

This traffic is saved to the news table and is saved with other news information.
This data changes very quickly, the cache is not very meaningful.
If every time the user views the news, go to the database to perform an update visitcount+1 operation,
It must have been more laborious.
Users must wait for the update to complete before they can see the content of the news.

After using Memcacheq, we can log every access to the message queue and then periodically update the database in the background.
The speed of writing message queues is much faster than updating MySQL directly.

In the viewnews.php:

<?PHP//Memcacheq config$queueOptions=Array(    ' Name ' = ' example-queue ', ' driveroptions ' =Array(        ' Host ' = ' 127.0.0.1 ', ' port ' = 22201    )); //Instantiate Zend_queue$queue=NewZend_queue (' Memcacheq ',$queueOptions); //Find out if the queue existsif(!$queue->getadapter ()->isexists ($queueOptions[' Name '])){    //If not, create it    $queue->createqueue ($queueOptions[' Name ']);} //Build A query string (key=val&key=val) because we need a scalar value//user visited $timestamp page in $page. $params=Http_build_query(    Array(     ' Timestamp ' =$timestamp, ' page ' =$page    ));//Send The data to the queue$queue->send ($params);


This saves this access to Message Queuing [Example-queue].

And then a cron, to handle the messages in the queue.

<?PHP//Memcacheq config$queueOptions=Array(    ' Name ' = ' example-queue ', ' driveroptions ' =Array(        ' Host ' = ' 127.0.0.1 ', ' port ' = 22201    )); //Instantiate Zend_queue$queue=NewZend_queue (' Memcacheq ',$queueOptions); //Retrieve 5 Items from the queue$messages=$queue->receive (5); //$message is now a instance of zend_queue_message_iterator//@TODO: Use a nice filteriterator;)foreach($messages  as $job){    if(' creating queue ' = =$job->body | |false===$job-body) {        //Skip Message        Continue; }    //Parse the query string to a array    Parse_str($job->body,$data); //Execute The heavy process//update database    $this->registerhit ($data[' Timestamp '],$data[' Page ']);}

Memcacheq of the "Go" persistence message queue

Related Article

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.