_php tutorial on Memcache-based queue implementation code written in PHP

Source: Internet
Author: User
PHP Class Code:
Copy CodeThe 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_mq ', 0);//clean all DATA
Define (' Is_add ', 0);//set DATA
$mobj = MQ::SETUPMQ (' 127.0.0.1 ', ' 11211 ');
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::D ebug ();
?>

How to use
Copy CodeThe code is as follows:
MQ::SETUPMQ (' 127.0.0.1 ', ' 11211 ');//Connection
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);//Remove a certain amount of data

http://www.bkjia.com/PHPjc/324519.html www.bkjia.com true http://www.bkjia.com/PHPjc/324519.html techarticle PHP Class Code: Copy code as follows: PHP class mq{public static $client; private static $m _real; private static $m _front; private stat IC $m _data = array (); Const Queue_max_num ...

  • 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.