php Memcache 中实现消息队列


Posted in PHP onNovember 24, 2009

对于一个很大的消息队列,频繁进行进行大数据库的序列化 和 反序列化,有太耗费。下面是我用PHP 实现的一个消息队列,只需要在尾部插入一个数据,就操作尾部,不用操作整个消息队列进行读取,与操作。但是,这个消息队列不是线程安全的,我只是尽量的避免了冲突的可能性。如果消息不是非常的密集,比如几秒钟才一个,还是可以考虑这样使用的。
如果你要实现线程安全的,一个建议是通过文件进行锁定,然后进行操作。下面是代码:

class Memcache_Queue 
{ 
private $memcache; 
private $name; 
private $prefix; 
function __construct($maxSize, $name, $memcache, $prefix = "__memcache_queue__") 
{ 
if ($memcache == null) { 
throw new Exception("memcache object is null, new the object first."); 
} 
$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; 
} 
function isEmpty() 
{ 
return $this->size == 0; 
} 
function isFull() 
{ 
return $this->size == $this->maxSize; 
} 
function enQueue($data) 
{ 
if ($this->isFull()) { 
throw new Exception("Queue is Full"); 
} 
$this->increment("size"); 
$this->set($this->real, $data); 
$this->set("real", ($this->real + 1) % $this->maxSize); 
return $this; 
} 
function deQueue() 
{ 
if ($this->isEmpty()) { 
throw new Exception("Queue is Empty"); 
} 
$this->decrement("size"); 
$this->delete($this->front); 
$this->set("front", ($this->front + 1) % $this->maxSize); 
return $this; 
} 
function getTop() 
{ 
return $this->get($this->front); 
} 
function getAll() 
{ 
return $this->getPage(); 
} 
function getPage($offset = 0, $limit = 0) 
{ 
if ($this->isEmpty() || $this->size < $offset) { 
return null; 
} 
$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 ($limit > 0 && $limit == $num) { 
break; 
} 
} 
return array_values($this->memcache->get($keys)); 
} 
function makeEmpty() 
{ 
$keys = $this->getAllKeys(); 
foreach ($keys as $value) { 
$this->delete($value); 
} 
$this->delete("real"); 
$this->delete("front"); 
$this->delete("size"); 
$this->delete("maxSize"); 
} 
private function getAllKeys() 
{ 
if ($this->isEmpty()) 
{ 
return array(); 
} 
$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; 
} 
private function add($pos, $data) 
{ 
$this->memcache->add($this->getKeyByPos($pos), $data); 
return $this; 
} 
private function increment($pos) 
{ 
return $this->memcache->increment($this->getKeyByPos($pos)); 
} 
private function decrement($pos) 
{ 
$this->memcache->decrement($this->getKeyByPos($pos)); 
} 
private function set($pos, $data) 
{ 
$this->memcache->set($this->getKeyByPos($pos), $data); 
return $this; 
} 
private function get($pos) 
{ 
return $this->memcache->get($this->getKeyByPos($pos)); 
} 
private function delete($pos) 
{ 
return $this->memcache->delete($this->getKeyByPos($pos)); 
} 
private function getKeyByPos($pos) 
{ 
return $this->prefix . $this->name . $pos; 
} 
}
PHP 相关文章推荐
一步一步学习PHP(4) php 函数 补充2
Feb 15 PHP
PHP 模拟$_PUT实现代码
Mar 15 PHP
php中hashtable实现示例分享
Feb 13 PHP
CI(CodeIgniter)框架配置
Jun 10 PHP
PHP面向对象程序设计之接口用法
Aug 20 PHP
php给一组指定关键词添加span标签的方法
Mar 31 PHP
php使用GD实现颜色渐变实例
Jun 02 PHP
常见PHP数据库解决方案分析介绍
Sep 24 PHP
PHP计算数组中值的和与乘积的方法(array_sum与array_product函数)
Apr 01 PHP
PHP对象实例化单例方法
Jan 19 PHP
phpMyAdmin无法登陆的解决方法
Apr 27 PHP
PHP 返回数组后处理方法(开户成功后弹窗提示)
Jul 03 PHP
phplock(php进程锁) v1.0 beta1
Nov 24 #PHP
PHP 进程锁定问题分析研究
Nov 24 #PHP
PHP 递归效率分析
Nov 24 #PHP
PHP 单引号与双引号的区别
Nov 24 #PHP
PHP小程序自动提交到自助友情连接
Nov 24 #PHP
php 引用(&amp;)详解
Nov 20 #PHP
php+javascript的日历控件
Nov 19 #PHP
You might like
解析php中如何调用用户自定义函数
2013/08/06 PHP
php实现保存submit内容之后禁止刷新
2014/03/19 PHP
php简单smarty入门程序实例
2015/06/11 PHP
CodeIgniter配置之routes.php用法实例分析
2016/01/19 PHP
php倒计时出现-0情况的解决方法
2016/07/28 PHP
详解php框架Yaf路由重写
2017/06/20 PHP
老生常谈PHP中的数据结构:DS扩展
2017/07/17 PHP
php对微信支付回调处理的方法
2018/08/23 PHP
PHP结合Ffmpeg快速搭建流媒体服务的实践记录
2018/10/31 PHP
Laravel validate error处理,ajax,json示例
2019/10/25 PHP
图片完美缩放
2006/09/07 Javascript
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
javascript 三种编解码方式
2010/02/01 Javascript
跨浏览器的 mouseenter mouseleave 以及 compareDocumentPosition的使用说明
2010/05/04 Javascript
jquery中dom操作和事件的实例学习-表单验证
2011/11/30 Javascript
jQuery 1.8 Release版本发布了
2012/08/14 Javascript
js日期相关函数总结分享
2013/10/15 Javascript
Js判断参数(String,Array,Object)是否为undefined或者值为空
2013/11/04 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
jQuery实现的给图片点赞+1动画效果(附在线演示及demo源码下载)
2015/12/31 Javascript
jQuery实现图片向左向右切换效果的简单实例
2016/05/18 Javascript
jQuery+CSS实现简单切换菜单示例
2016/07/27 Javascript
nodejs中方法和模块用法示例
2018/12/24 NodeJs
javascript实现贪吃蛇经典游戏
2020/04/10 Javascript
利用 Monkey 命令操作屏幕快速滑动
2016/12/07 Python
CentOS中升级Python版本的方法详解
2017/07/10 Python
python使用Tesseract库识别验证
2018/03/21 Python
Python使用random.shuffle()打乱列表顺序的方法
2018/11/08 Python
Python docx库用法示例分析
2019/02/16 Python
Paul’s Boutique官网:英国时尚手袋品牌
2018/03/31 全球购物
美国家庭鞋店:Shoe Sensation
2019/09/27 全球购物
高二美术教学反思
2014/01/14 职场文书
初中学生期末评语
2014/04/24 职场文书
关于读书的演讲稿1000字
2014/08/27 职场文书
课外访万家心得体会
2014/09/03 职场文书
党的群众路线调研报告
2014/11/03 职场文书