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 Session变量不能传送到下一页的解决方法
Nov 27 PHP
PHP技术开发技巧分享
Mar 23 PHP
PHP抽象类 介绍
Jun 13 PHP
php排序算法(冒泡排序,快速排序)
Oct 09 PHP
关于使用key/value数据库redis和TTSERVER的心得体会
Jun 28 PHP
php四种基础算法代码实例
Oct 29 PHP
PHP中array_map与array_column之间的关系分析
Aug 19 PHP
php页面函数设置超时限制的方法
Dec 01 PHP
php获取系统变量方法小结
May 29 PHP
一张表搞清楚php is_null、empty、isset的区别
Jul 07 PHP
PDO::rollBack讲解
Jan 29 PHP
PHP htmlspecialchars_decode()函数用法讲解
Mar 01 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
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
图文详解phpstorm配置Xdebug进行调试PHP教程
2016/06/13 PHP
PHP单例模式模拟Java Bean实现方法示例
2018/12/07 PHP
JavaScript 自动分号插入(JavaScript synat:auto semicolon insertion)
2009/11/04 Javascript
用JQuery调用Session的实现代码
2010/10/29 Javascript
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
2013/05/24 Javascript
js实现文本框中焦点在最后位置
2014/03/04 Javascript
JS实现表格数据各种搜索功能的方法
2015/03/03 Javascript
jquery实现清新实用的网页菜单效果
2015/08/28 Javascript
基于BootStarp的Dailog
2016/04/28 Javascript
JS实现点击事件统计的简单实例
2016/07/10 Javascript
详解NODEJS基于FFMPEG视频推流测试
2017/11/17 NodeJs
vue裁切预览组件功能的实现步骤
2018/05/04 Javascript
详解Vue项目中出现Loading chunk {n} failed问题的解决方法
2018/09/14 Javascript
JavaScript 对引擎、运行时、调用堆栈的概述理解
2018/10/22 Javascript
JavaScript设计模式之观察者模式实例详解
2019/01/16 Javascript
layui默认选中table的CheckBox复选框方法
2019/09/19 Javascript
echarts 使用formatter 修改鼠标悬浮事件信息操作
2020/07/20 Javascript
python使用urlparse分析网址中域名的方法
2015/04/15 Python
Python函数中的函数(闭包)用法实例
2016/03/15 Python
Python遍历文件夹和读写文件的实现方法
2017/05/10 Python
Python实现中文数字转换为阿拉伯数字的方法示例
2017/05/26 Python
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
2018/01/26 Python
Flask模拟实现CSRF攻击的方法
2018/07/24 Python
Django框架中序列化和反序列化的例子
2019/08/06 Python
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
2020/02/24 Python
python实现对变位词的判断方法
2020/04/05 Python
日本PLST在线商店:日本时尚杂志刊载的人气服装
2016/12/10 全球购物
历史学专业毕业生求职信
2013/09/27 职场文书
商务英语专业毕业生自荐信
2013/11/05 职场文书
国际贸易个人求职信范文
2014/01/04 职场文书
祖国在我心中演讲稿500字
2014/05/04 职场文书
先进集体事迹材料范文
2014/12/25 职场文书
成绩单家长意见
2015/06/03 职场文书
地道战观后感300字
2015/06/04 职场文书
JavaScript实现两个数组的交集
2022/03/25 Javascript