




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文格式为Word版,下载可任意编辑——phpMemcache中实现消息队列phpMemcache中实现消息队列
对于一个很大的消息队列,频繁举行举行大数据库的序列化和反序列化,有太花费。下面是我用PHP实现的一个消息队列,只需要在尾部插入一个数据,就操作尾部,不用操作整个消息队列举行读取,与操作。但是,这个消息队列不是线程安好的,我只是尽量的制止了冲突的`可能性。假设消息不是分外的密集,譬如几秒钟才一个,还是可以考虑这样使用的。就跟随我一起去了解下吧,想了解更多相关信息请持续关注我们我!
假设你要实现线程安好的,一个建议是通过文件举行锁定,然后举行操作。下面是代码:
复制代码代码如下:
classMemcache_Queue
private$memcache;
private$name;
private$prefix;
function__construct$maxSize,$name,$memcache,$prefix=__memcache_queue__
if$memcache==null
thrownewExceptionmemcacheobjectisnull,newtheobjectfirst.;
$this-memcache=$memcache;
$this-name=$name;
$this-prefix=$prefix;
$this-maxSize=$maxSize;
$this-front=0;
$this-real=0;
$this-size=0;
function__get$name
return$this-get$name;
function__set$name,$value
$this-add$name,$value;
return$this;
functionisEmpty
return$this-size==0;
functionisFull
return$this-size==$this-maxSize;
functionenQueue$data
if$this-isFull
thrownewExceptionQueueisFull;
$this-incrementsize;
$this-set$this-real,$data;
$this-setreal,$this-real+1%$this-maxSize;
return$this;
functiondeQueue
if$this-isEmpty
thrownewExceptionQueueisEmpty;
$this-decrementsize;
$this-delete$this-front;
$this-setfront,$this-front+1%$this-maxSize;
return$this;
functiongetTop
return$this-get$this-front;
functiongetAll
return$this-getPage;
functiongetPage$offset=0,$limit=0
if$this-isEmpty||$this-size$offset
returnnull;
$keys[]=$this-getKeyByPos$this-front+$offset%$this-maxSize;
$num=1;
for$pos=$this-front+$offset+1%$this-maxSize;$pos!=$this-real;$pos=$pos+1%$this-maxSize
$keys[]=$this-getKeyByPos$pos;
$num++;
if$limit0$limit==$num
break;
returnarray_values$this-memcache-get$keys;
functionmakeEmpty
$keys=$this-getAllKeys;
foreach$keysas$value
$this-delete$value;
$this-deletereal;
$this-deletefront;
$this-deletesize;
$this-deletemaxSize;
privatefunctiongetAllKeys
if$this-isEmpty
returnarray;
$keys[]=$this-getKeyByPos$this-front;
for$pos=$this-front+1%$this-maxSize;$pos!=$this-real;$pos=$pos+1%$this-maxSize
$keys[]=$this-getKeyByPos$pos;
return$keys;
privatefunctionadd$pos,$data
$this-memcache-add$this-getKeyByPos$pos,$data;
return$this;
privatefunctionincrement$pos
return$this-memcache-increment$this-getKeyByPos$pos;
privatefunctiondecrement$pos
$this-memcache-decrement$this-getKeyByPos$pos;
privatefunctionset$pos,$data
$this-memcache-set$this-getKeyByPos$pos,$data;
return$this;
privatefunctionget$pos
return$this-memcache-get$this-getKeyByPos$pos;
privatefunctionde
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论