用PHP写的基于Memcache的Queue实现代码
PHP类代码: @H_403_0@<div class="codetitle"><a style="CURSOR: pointer" data="31644" class="copybut" id="copybut31644" onclick="doCopy('code31644')"> 代码如下:
<div class="codebody" id="code31644"> @H_
403_0@<?
PHP @H_
4030@class MQ{ @H4030@public static $client; @H403_0@private static $m
real; @H403_0@private static $m
front; @H403_0@private static $m
data = array(); @H403_0@const QUEUE_MAX
NUM = 100000000; @H403_0@const QUEUE_FRONT_KEY = '_queue_item
front'; @H403_0@const QUEUE_REAL_KEY = '_queue_item
real'; @H4030@public static function setupMq($conf) { @H403_0@self::$client = memcache
pconnect($conf); @H403_0@self::$m_real = memcache_get(self::$client,self::QUEUE_REAL
KEY); @H403_0@self::$m_front = memcache_get(self::$client,self::QUEUE_FRONT
KEY); @H403_0@if (!isset(self::$m_real) || emptyempty(self::$m
real)) { @H4030@self::$real= 0; @H4030@} @H403_0@if (!isset(self::$m_front) || emptyempty(self::$m
front)) { @H403_0@self::$m
front = 0; @H4030@} @H4030@return self::$client; @H4030@} @H4030@public static function add($queue,$data) { @H4030@$result = false; @H403_0@if (self::$m_real < self::QUEUE_MAX
NUM) { @H403_0@if (memcache_add(self::$client,$queue.self::$m
real,$data)) { @H4030@self::mqRealChange(); @H4030@$result = true; @H4030@} @H4030@} @H4030@return $result; @H4030@} @H4030@public static function get($key,$count) { @H4030@$num = 0; @H403_0@for ($i=self::$m_front;$i<self::$m
front + $count;$i++) { @H403_0@if ($dataTmp = memcache
get(self::$client,$key.$i)) { @H403_0@self::$m
data[] = $dataTmp; @H403_0@memcache
delete(self::$client,$key.$i); @H4030@$num++; @H4030@} @H4030@} @H4030@if ($num>0) { @H4030@self::mqFrontChange($num); @H4030@} @H403_0@return self::$m
data; @H4030@} @H4030@private static function mqRealChange() { @H403_0@memcache_add(self::$client,self::QUEUE_REAL
KEY,0); @H403_0@self::$m_real = memcache
increment(self::$client,1); @H4030@} @H4030@private static function mqFrontChange($num) { @H403_0@memcache_add(self::$client,self::QUEUE_FRONT
KEY,0); @H403_0@self::$m_front = memcache
increment(self::$client,$num); @H4030@} @H403_0@public static function mflush($memcache
obj) { @H403_0@memcache_flush($memcache
obj); @H4030@} @H4030@public static function Debug() { @H403_0@echo 'real:'.self::$m
real."
/r/n"; @H403_0@echo 'front:'.self::$m
front."
/r/n"; @H403_0@echo 'wait for process data:'.intval(self::$m_real - self::$m
front); @H4030@echo "
/r/n"; @H4030@echo ''; @H
403_0@print_r(self::$m
data); @H4030@echo ''; @H
4030@} @H4030@} @H403_0@define('FLUSH
MQ',0);//CLEAN ALL DATA @H403_0@define('IS
ADD',0);//SET DATA @H4030@$mobj = MQ::setupMq('127.0.0.1','11211'); @H403_0@if (FLUSH
MQ) { @H4030@MQ::mflush($mobj); @H4030@} else { @H403_0@if (IS
ADD) { @H403_0@MQ::add('user
sync','1test'); @H403_0@MQ::add('user
sync','2test'); @H403_0@MQ::add('user
sync','3test'); @H403_0@MQ::add('user
sync','4test'); @H403_0@MQ::add('user
sync','5test'); @H403_0@MQ::add('user
sync','6test'); @H4030@} else { @H403_0@MQ::get('user
sync',10); @H4030@} @H4030@} @H4030@MQ::Debug(); @H4030@?> @H4030@