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网站在线人数统计
Apr 09 PHP
php 显示指定路径下的图片
Oct 29 PHP
php缓冲 output_buffering的使用详解
Jun 13 PHP
探讨:如何通过stats命令分析Memcached的内部状态
Jun 14 PHP
PHP中把stdClass Object转array的几个方法
May 08 PHP
PHP实现路由映射到指定控制器
Aug 13 PHP
PHP测试框架PHPUnit组织测试操作示例
May 28 PHP
php post json参数的传递和接收处理方法
May 31 PHP
PHP设计模式之委托模式定义与用法简单示例
Aug 13 PHP
PHP7中I/O模型内核剖析详解
Apr 14 PHP
php日志函数error_log用法实例分析
Sep 23 PHP
PHP 时间处理类Carbon
May 20 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中global和$GLOBALS[]的分析之一
2012/02/02 PHP
PHP使用自定义方法实现数组合并示例
2016/07/07 PHP
Yii2验证器(Validator)用法分析
2016/07/23 PHP
50款非常棒的 jQuery 插件分享
2012/03/29 Javascript
表单元素的submit()方法和onsubmit事件应用概述
2013/02/01 Javascript
js控制浏览器全屏示例代码
2014/02/20 Javascript
js实现简单鼠标跟随效果的方法
2015/04/10 Javascript
jquery自定义插件开发之window的实现过程
2016/05/06 Javascript
three.js中文文档学习之通过模块导入
2017/11/20 Javascript
细说webpack源码之compile流程-入口函数run
2017/12/26 Javascript
Angularjs实现页面模板清除的方法
2018/07/20 Javascript
ionic使用angularjs表单验证(模板验证)
2018/12/12 Javascript
微信小程序实现页面浮动导航
2019/01/28 Javascript
Angular实现svg和png图片下载实现
2019/05/05 Javascript
详解vue 组件的实现原理
2020/11/12 Javascript
利用python实现xml与数据库读取转换的方法
2017/06/17 Python
Python开发的HTTP库requests详解
2017/08/29 Python
Python中用post、get方式提交数据的方法示例
2017/09/22 Python
python增加矩阵维度的实例讲解
2018/04/04 Python
python-web根据元素属性进行定位的方法
2019/12/13 Python
Pytorch的mean和std调查实例
2020/01/02 Python
pycharm 2019 最新激活方式(pycharm破解、激活)
2020/09/22 Python
Numpy中ndim、shape、dtype、astype的用法详解
2020/06/14 Python
python 6行代码制作月历生成器
2020/09/18 Python
美国购车网站:TrueCar
2016/10/19 全球购物
Fanatics法国官网:美国体育电商
2019/08/27 全球购物
标准导师推荐信(医学类)
2013/10/28 职场文书
公司财务总监岗位职责
2013/12/14 职场文书
演讲稿格式
2014/04/30 职场文书
关于教师节的演讲稿
2014/09/04 职场文书
工地食品安全责任书
2015/05/09 职场文书
小学科学课教学反思
2016/02/23 职场文书
低端且暴利的线上线下创业项目分享
2019/09/03 职场文书
vue实现可拖拽的dialog弹框
2021/05/13 Vue.js
Apache Hudi数据布局黑科技降低一半查询时间
2022/03/31 Servers
微信告警的zabbix监控系统 监控整个NGINX集群
2022/04/18 Servers