PHP实现的memcache环形队列类实例


Posted in PHP onJuly 28, 2015

本文实例讲述了PHP实现的memcache环形队列类。分享给大家供大家参考。具体如下:

这里介绍了PHP实现的memcache环形队列类。没咋学过数据结构,因为业务需要,所以只是硬着头皮模拟的! 参考PHP memcache 队列代码。为使队列随时可入可出,且不受int长度越界危险(单链采取Head自增的话不作处理有越界可能),所以索性改写成环形队列。可能还有BUG,忘见谅!

<?php
/**
 * PHP memcache 环形队列类
 * 原作者 LKK/lianq.net
 * 修改 FoxHunter
 * 因业务需要只保留的队列中的Pop和Push,修改过期时间为0即永久
 */
class MQueue
{
 public static $client;
 private $expire; //过期时间,秒,1~2592000,即30天内
 private $sleepTime; //等待解锁时间,微秒
 private $queueName; //队列名称,唯一值
 private $retryNum; //尝试次数
 private $MAXNUM; //最大队列容量
 private $canRewrite; //是否可以覆写开关,满出来的内容从头部开始覆盖重写原来的数据
 private $HEAD; //下一步要进入的指针位置
 private $TAIL; //下一步要进入的指针位置
 private $LEN; //队列现有长度
 const LOCK_KEY = '_Fox_MQ_LOCK_'; //锁存储标示
 const LENGTH_KEY = '_Fox_MQ_LENGTH_'; //队列现长度存储标示
 const VALU_KEY = '_Fox_MQ_VAL_'; //队列键值存储标示
 const HEAD_KEY = '_Fox_MQ_HEAD_'; //队列HEAD指针位置标示
 const TAIL_KEY = '_Fox_MQ_TAIL_'; //队列TAIL指针位置标示
 /*
  * 构造函数
  * 对于同一个$queueName,实例化时必须保障构造函数的参数值一致,否则pop和push会导队列顺序混乱
  */
 public function __construct($queueName = '', $maxqueue = 1, $canRewrite = false, $expire = 0, $config = '')
 {
  if (empty($config)) {
   self::$client = memcache_pconnect('127.0.0.1', 11211);
  } elseif (is_array($config)) { //array('host'=>'127.0.0.1','port'=>'11211')
   self::$client = memcache_pconnect($config['host'], $config['port']);
  } elseif (is_string($config)) { //"127.0.0.1:11211"
   $tmp   = explode(':', $config);
   $conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1';
   $conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211';
   self::$client = memcache_pconnect($conf['host'], $conf['port']);
  }
  if (!self::$client)
   return false;
  ignore_user_abort(true); //当客户断开连接,允许继续执行
  set_time_limit(0); //取消脚本执行延时上限
  $this->access  = false;
  $this->sleepTime = 1000;
  $expire   = (empty($expire)) ? 0 : (int) $expire + 1;
  $this->expire  = $expire;
  $this->queueName = $queueName;
  $this->retryNum = 20000;
  $this->MAXNUM  = $maxqueue != null ? $maxqueue : 1;
  $this->canRewrite = $canRewrite;
  $this->getHeadAndTail();
  if (!isset($this->HEAD) || empty($this->HEAD))
   $this->HEAD = 0;
  if (!isset($this->TAIL) || empty($this->TAIL))
   $this->TAIL = 0;
  if (!isset($this->LEN) || empty($this->LEN))
   $this->LEN = 0;
 }
 //获取队列首尾指针信息和长度
 private function getHeadAndTail()
 {
  $this->HEAD = (int) memcache_get(self::$client, $this->queueName . self::HEAD_KEY);
  $this->TAIL = (int) memcache_get(self::$client, $this->queueName . self::TAIL_KEY);
  $this->LEN = (int) memcache_get(self::$client, $this->queueName . self::LENGTH_KEY);
 }
 // 利用memcache_add原子性加锁
 private function lock()
 {
  if ($this->access === false) {
   $i = 0;
   while (!memcache_add(self::$client, $this->queueName . self::LOCK_KEY, 1, false, $this->expire)) {
    usleep($this->sleepTime);
    @$i++;
    if ($i > $this->retryNum) { //尝试等待N次
     return false;
     break;
    }
   }
   return $this->access = true;
  }
  return false;
 }
 //更新头部指针指向,指向下一个位置
 private function incrHead()
 {
  //$this->getHeadAndTail(); //获取最新指针信息 ,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释
  $this->HEAD++; //头部指针下移
  if ($this->HEAD >= $this->MAXNUM) {
   $this->HEAD = 0; //边界值修正
  }
  ;
  $this->LEN--; //Head的移动由Pop触发,所以相当于数量减少
  if ($this->LEN < 0) {
   $this->LEN = 0; //边界值修正
  }
  ;
  memcache_set(self::$client, $this->queueName . self::HEAD_KEY, $this->HEAD, false, $this->expire); //更新
  memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
 }
 //更新尾部指针指向,指向下一个位置
 private function incrTail()
 {
  //$this->getHeadAndTail(); //获取最新指针信息,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释
  $this->TAIL++; //尾部指针下移
  if ($this->TAIL >= $this->MAXNUM) {
   $this->TAIL = 0; //边界值修正
  }
  ;
  $this->LEN++; //Head的移动由Push触发,所以相当于数量增加
  if ($this->LEN >= $this->MAXNUM) {
   $this->LEN = $this->MAXNUM; //边界值长度修正
  }
  ;
  memcache_set(self::$client, $this->queueName . self::TAIL_KEY, $this->TAIL, false, $this->expire); //更新
  memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
 }
 // 解锁
 private function unLock()
 {
  memcache_delete(self::$client, $this->queueName . self::LOCK_KEY);
  $this->access = false;
 }
 //判断是否满队列
 public function isFull()
 {
  //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  if ($this->canRewrite)
   return false;
  return $this->LEN == $this->MAXNUM ? true : false;
 }
 //判断是否为空
 public function isEmpty()
 {
  //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  return $this->LEN == 0 ? true : false;
 }
 public function getLen()
 {
  //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  return $this->LEN;
 }
 /*
  * push值
  * @param mixed 值
  * @return bool
  */
 public function push($data = '')
 {
  $result = false;
  if (empty($data))
   return $result;
  if (!$this->lock()) {
   return $result;
  }
  $this->getHeadAndTail(); //获取最新指针信息
  if ($this->isFull()) { //只有在非覆写下才有Full概念
   $this->unLock();
   return false;
  }
  if (memcache_set(self::$client, $this->queueName . self::VALU_KEY . $this->TAIL, $data, MEMCACHE_COMPRESSED, $this->expire)) {
   //当推送后,发现尾部和头部重合(此时指针还未移动),且右边仍有未由Head读取的数据,那么移动Head指针,避免尾部指针跨越Head
   if ($this->TAIL == $this->HEAD && $this->LEN >= 1) {
    $this->incrHead();
   }
   $this->incrTail(); //移动尾部指针
   $result = true;
  }
  $this->unLock();
  return $result;
 }
 /*
  * Pop一个值
  * @param [length] int 队列长度
  * @return array
  */
 public function pop($length = 0)
 {
  if (!is_numeric($length))
   return false;
  if (!$this->lock())
   return false;
  $this->getHeadAndTail();
  if (empty($length))
   $length = $this->LEN; //默认读取所有
  if ($this->isEmpty()) {
   $this->unLock();
   return false;
  }
  //获取长度超出队列长度后进行修正
  if ($length > $this->LEN)
   $length = $this->LEN;
  $data = $this->popKeyArray($length);
  $this->unLock();
  return $data;
 }
 /*
  * pop某段长度的值
  * @param [length] int 队列长度
  * @return array
  */
 private function popKeyArray($length)
 {
  $result = array();
  if (empty($length))
   return $result;
  for ($k = 0; $k < $length; $k++) {
   $result[] = @memcache_get(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD);
   @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD, 0);
   //当提取值后,发现头部和尾部重合(此时指针还未移动),且右边没有数据,即队列中最后一个数据被完全掏空,此时指针停留在本地不移动,队列长度变为0
   if ($this->TAIL == $this->HEAD && $this->LEN <= 1) {
    $this->LEN = 0;
    memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
    break;
   } else {
    $this->incrHead(); //首尾未重合,或者重合但是仍有未读取出的数据,均移动HEAD指针到下一处待读取位置
   }
  }
  return $result;
 }
 /*
  * 重置队列
  * * @return NULL
  */
 private function reset($all = false)
 {
  if ($all) {
   memcache_delete(self::$client, $this->queueName . self::HEAD_KEY, 0);
   memcache_delete(self::$client, $this->queueName . self::TAIL_KEY, 0);
   memcache_delete(self::$client, $this->queueName . self::LENGTH_KEY, 0);
  } else {
   $this->HEAD = $this->TAIL = $this->LEN = 0;
   memcache_set(self::$client, $this->queueName . self::HEAD_KEY, 0, false, $this->expire);
   memcache_set(self::$client, $this->queueName . self::TAIL_KEY, 0, false, $this->expire);
   memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, 0, false, $this->expire);
  }
 }
 /*
  * 清除所有memcache缓存数据
  * @return NULL
  */
 public function memFlush()
 {
  memcache_flush(self::$client);
 }
 public function clear($all = false)
 {
  if (!$this->lock())
   return false;
  $this->getHeadAndTail();
  $Head = $this->HEAD;
  $Length = $this->LEN;
  $curr = 0;
  for ($i = 0; $i < $Length; $i++) {
   $curr = $this->$Head + $i;
   if ($curr >= $this->MAXNUM) {
    $this->HEAD = $curr = 0;
   }
   @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $curr, 0);
  }
  $this->unLock();
  $this->reset($all);
  return true;
 }
}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
一个自定义位数的php多用户计数器代码
Mar 11 PHP
PHP session常见问题集锦及解决办法总结
Mar 18 PHP
php 取得瑞年与平年的天数的代码
Aug 10 PHP
php安全开发 添加随机字符串验证,防止伪造跨站请求
Feb 14 PHP
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
May 04 PHP
php中socket的用法详解
Oct 24 PHP
Laravel 5 框架入门(一)
Apr 09 PHP
php求一个网段开始与结束IP地址的方法
Jul 09 PHP
ThinkPHP路由机制简介
Mar 23 PHP
详解Yii2.0使用AR联表查询实例
Jun 16 PHP
php递归函数怎么用才有效
Feb 24 PHP
php的对象传值与引用传值代码实例讲解
Feb 26 PHP
如何实现php图片等比例缩放
Jul 28 #PHP
PHP封装CURL扩展类实例
Jul 28 #PHP
php图像处理类实例
Jul 28 #PHP
图文介绍PHP添加Redis模块及连接
Jul 28 #PHP
PHP生成树的方法
Jul 28 #PHP
php计算税后工资的方法
Jul 28 #PHP
怎样搭建PHP开发环境
Jul 28 #PHP
You might like
php 伪造本地文件包含漏洞的代码
2011/11/03 PHP
ThinkPHP3.1新特性之动态设置自动完成和自动验证示例
2014/06/19 PHP
php对文件夹进行相关操作(遍历、计算大小)
2015/11/04 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
2016/01/07 PHP
PHP大文件切割上传功能实例分析
2019/07/01 PHP
JS input 数字验证代码
2009/07/30 Javascript
JS去除右边逗号的简单方法
2013/07/03 Javascript
JS预览图像将本地图片显示到浏览器上
2013/08/25 Javascript
JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
2014/12/20 Javascript
js实现刷新iframe的方法汇总
2015/04/27 Javascript
14款经典网页图片和文字特效的jQuery插件-前端开发必备
2015/08/25 Javascript
纯JS实现只能输入数字的简单代码
2017/06/21 Javascript
JavaScript实现AOP详解(面向切面编程,装饰者模式)
2017/12/19 Javascript
vue脚手架及vue-router基本使用
2018/04/09 Javascript
vue中使用[provide/inject]实现页面reload的方法
2019/09/30 Javascript
vue实现数据控制视图的原理解析
2020/01/07 Javascript
python optparse模块使用实例
2015/04/09 Python
Python温度转换实例分析
2018/01/17 Python
用十张图详解TensorFlow数据读取机制(附代码)
2018/02/06 Python
Python 十六进制整数与ASCii编码字符串相互转换方法
2018/07/09 Python
python使用folium库绘制地图点击框
2018/09/21 Python
python生成n个元素的全组合方法
2018/11/13 Python
Python脚本修改阿里云的访问控制列表的方法
2019/03/08 Python
python如何通过pyqt5实现进度条
2020/01/20 Python
Python中使用filter过滤列表的一个小技巧分享
2020/05/02 Python
html5中为audio标签增加停止按钮动作实现方法
2013/01/04 HTML / CSS
Hotels.com爱尔兰:全球酒店预订
2017/02/24 全球购物
单身联谊活动方案
2014/01/29 职场文书
党代会心得体会
2014/09/04 职场文书
个人投资合作协议书
2014/10/12 职场文书
王兆力在市委党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
2015年效能监察工作总结
2015/04/23 职场文书
心理健康教育培训研修感言
2015/11/18 职场文书
《圆的周长》教学反思
2016/02/17 职场文书
超级实用的公文标题大全!
2019/07/19 职场文书
2019公司借款合同范本2篇!
2019/07/24 职场文书