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设计模式 Mediator (中介者模式)
Jun 26 PHP
php Calender(日历)代码分享
Jan 03 PHP
CodeIgniter输出中文乱码的两种解决办法
Jun 12 PHP
CI框架给视图添加动态数据
Dec 01 PHP
基于PHP实现等比压缩图片大小
Mar 04 PHP
PHP使用SOAP扩展实现WebService的方法
Apr 01 PHP
PHP购物车类Cart.class.php定义与用法示例
Jul 20 PHP
PHP利用超级全局变量$_GET来接收表单数据的实例
Nov 05 PHP
详谈php中 strtr 和 str_replace 的效率问题
May 14 PHP
详解Yii2 之 生成 URL 的方法
Jun 16 PHP
php微信支付之公众号支付功能
May 30 PHP
详解阿里云视频直播PHP-SDK接入教程
Jul 09 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在线生成二维码代码(google api)
2013/06/03 PHP
ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
2014/07/01 PHP
PHP 使用redis简单示例分享
2015/03/05 PHP
PHP+MySQL存储数据常见中文乱码问题小结
2016/06/13 PHP
php+flash+jQuery多图片上传源码分享
2020/07/27 PHP
phpinfo的知识点总结
2019/10/10 PHP
Table冻结表头示例代码
2013/08/20 Javascript
基于jquery的9行js轻松实现tab控件示例
2013/10/12 Javascript
利用javascript实现全部删或清空所选的操作
2014/05/27 Javascript
AngularJS实现动态编译添加到dom中的方法
2016/11/04 Javascript
浅谈js使用in和hasOwnProperty获取对象属性的区别
2017/04/27 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
详解React 16 中的异常处理
2017/07/28 Javascript
node.js自动上传ftp的脚本分享
2018/06/16 Javascript
vue.js动画中的js钩子函数的实现
2018/07/06 Javascript
Angular动画实现的2种方式以及添加购物车动画实例代码
2018/08/09 Javascript
ES6的解构赋值实例详解
2019/05/06 Javascript
[46:02]DOTA2上海特级锦标赛D组资格赛#2 Liquid VS VP第二局
2016/02/28 DOTA
[01:09:10]NB vs Liquid Supermajor小组赛 A组胜者组决赛 BO3 第一场 6.2
2018/06/04 DOTA
python去掉行尾的换行符方法
2017/01/04 Python
Python实现抓取网页生成Excel文件的方法示例
2017/08/05 Python
解决Matplotlib图表不能在Pycharm中显示的问题
2018/05/24 Python
在macOS上搭建python环境的实现方法
2019/08/13 Python
python读取当前目录下的CSV文件数据
2020/03/11 Python
python实现音乐播放和下载小程序功能
2020/04/26 Python
Django静态文件加载失败解决方案
2020/08/26 Python
css 省略号 css3让多余的字符串消失并附加省略号的实现代码
2013/02/07 HTML / CSS
HTML5的结构和语义(2):结构
2008/10/17 HTML / CSS
西班牙在线药店:DosFarma
2020/03/28 全球购物
中文系师范生自荐信
2013/10/01 职场文书
公司成本主管岗位责任制
2014/02/21 职场文书
中华在我心中演讲稿
2014/09/13 职场文书
先进基层党组织事迹材料2016
2016/02/29 职场文书
nginx前后端同域名配置的方法实现
2021/03/31 Servers
浅谈redis五大数据结构和使用场景
2021/04/12 Redis
mongoDB数据库索引快速入门指南
2022/03/23 MongoDB