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