Memcache-based Queue implementation code written in PHP

Source: Internet
Author: User
Use the Memcache-based Queue code written in PHP. For more information, see. Memcache

Php code:
The code is as follows:
Class MQ {
Public static $ client;
Private static $ m_real;
Private static $ m_front;
Private static $ m_data = array ();
Const QUEUE_MAX_NUM = 100000000;
Const QUEUE_FRONT_KEY = '_ queue_item_front ';
Const QUEUE_REAL_KEY = '_ queue_item_real ';
Public static function setupMq ($ conf ){
Self: $ client = memcache_pconnect ($ conf );
Self: $ m_real = memcache_get (self: $ client, self: QUEUE_REAL_KEY );
Self: $ m_front = memcache_get (self: $ client, self: QUEUE_FRONT_KEY );
If (! Isset (self ::$ m_real) | emptyempty (self ::$ m_real )){
Self: $ real = 0;
}
If (! Isset (self ::$ m_front) | emptyempty (self ::$ m_front )){
Self: $ m_front = 0;
}
Return self: $ client;
}
Public static function add ($ queue, $ data ){
$ Result = false;
If (self ::$ m_real <self: QUEUE_MAX_NUM ){
If (memcache_add (self: $ client, $ queue. self: $ m_real, $ data )){
Self: mqRealChange ();
$ Result = true;
}
}
Return $ result;
}
Public static function get ($ key, $ count ){
$ Num = 0;
For ($ I = self: $ m_front; $ I If ($ dataTmp = memcache_get (self: $ client, $ key. $ I )){
Self: $ m_data [] = $ dataTmp;
Memcache_delete (self: $ client, $ key. $ I );
$ Num ++;
}
}
If ($ num> 0 ){
Self: mqFrontChange ($ num );
}
Return self: $ m_data;
}
Private static function mqRealChange (){
Memcache_add (self: $ client, self: QUEUE_REAL_KEY, 0 );
Self: $ m_real = memcache_increment (self: $ client, self: QUEUE_REAL_KEY, 1 );
}
Private static function mqFrontChange ($ num ){
Memcache_add (self: $ client, self: QUEUE_FRONT_KEY, 0 );
Self: $ m_front = memcache_increment (self: $ client, self: QUEUE_FRONT_KEY, $ num );
}
Public static function mflush ($ memcache_obj ){
Memcache_flush ($ memcache_obj );
}
Public static function Debug (){
Echo 'real: '. self: $ m_real ."
/R/n ";
Echo 'front: '. self: $ m_front ."
/R/n ";
Echo 'wait for process data: '. intval (self ::$ m_real-self ::$ m_front );
Echo"
/R/n ";
Echo'
';
Print_r (self: $ m_data );
Echo'
';
}
}
Define ('flush _ Rabbit', 0); // CLEAN ALL DATA
Define ('is _ add', 0); // SET DATA
$ Mobj = MQ: setupMq ('192. 0.0.1 ', '123 ');
If (FLUSH_MQ ){
MQ: mflush ($ mobj );
} Else {
If (IS_ADD ){
MQ: add ('User _ sync', '1test ');
MQ: add ('User _ sync', '2test ');
MQ: add ('User _ sync', '3test ');
MQ: add ('User _ sync', '4test ');
MQ: add ('User _ sync', '5test ');
MQ: add ('User _ sync', '6test ');
} Else {
MQ: get ('User _ sync', 10 );
}
}
MQ: Debug ();
?>

Usage
The code is as follows:
MQ: setupMq ('192. 0.0.1 ', '123'); // Connect
MQ: add ($ key, $ value); // add data to the queue
MQ: add ($ key, $ value); // add data to the queue
MQ: add ($ key, $ value); // add data to the queue
MQ: add ($ key, $ value); // add data to the queue
MQ: add ($ key, $ value); // add data to the queue
MQ: add ($ key, $ value); // add data to the queue
MQ: get ($ key, 10); // Retrieve a certain amount of data

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.