用PHP寫的基于Memcache的Queue實(shí)現(xiàn)代碼
更新時(shí)間:2011年11月27日 17:06:32 作者:
用PHP寫的基于Memcache的Queue實(shí)現(xiàn)代碼,需要的朋友可以參考下。
php類代碼:
<?php
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<self::$m_front + $count;$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."<br>/r/n";
echo 'front:'.self::$m_front."<br>/r/n";
echo 'wait for process data:'.intval(self::$m_real - self::$m_front);
echo "<br>/r/n";
echo '<pre>';
print_r(self::$m_data);
echo '<pre>';
}
}
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::Debug();
?>
使用方法
MQ::setupMq('127.0.0.1','11211');//連接
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ:get($key, 10);//取出一定數(shù)量的數(shù)據(jù)
復(fù)制代碼 代碼如下:
<?php
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<self::$m_front + $count;$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."<br>/r/n";
echo 'front:'.self::$m_front."<br>/r/n";
echo 'wait for process data:'.intval(self::$m_real - self::$m_front);
echo "<br>/r/n";
echo '<pre>';
print_r(self::$m_data);
echo '<pre>';
}
}
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::Debug();
?>
使用方法
復(fù)制代碼 代碼如下:
MQ::setupMq('127.0.0.1','11211');//連接
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ::add($key, $value);//添加數(shù)據(jù)到隊(duì)列
MQ:get($key, 10);//取出一定數(shù)量的數(shù)據(jù)
相關(guān)文章
PHP 多進(jìn)程與信號(hào)中斷實(shí)現(xiàn)多任務(wù)常駐內(nèi)存管理實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于PHP 多進(jìn)程與信號(hào)中斷實(shí)現(xiàn)多任務(wù)常駐內(nèi)存管理的相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-10-10
php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系
php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系...2006-11-11
php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例
這篇文章主要為大家介紹了php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)策略模式(strategy)
這篇文章主要介紹了php設(shè)計(jì)模式中的適配器模式,使用php實(shí)現(xiàn)適配器模式,感興趣的小伙伴們可以參考一下2015-12-12

