php的memcache类分享(memcache队列)_php实例

memcacheQueue.class.php

复制代码 代码如下:

<?php
/**
 * PHP memcache 队列类
 * @author LKK/lianq.net
 * @version 0.3
 * @修改说明:
 * 1.放弃了之前的AB面轮值思路,使用类似数组的构造,重写了此类.
 * 2.队列默认先进先出,但增加了反向读取功能.
 * 3.感谢网友FoxHunter提出的宝贵意见.
 * @example:
 * $obj = new memcacheQueue('duilie');
 * $obj->add('1asdf');
 * $obj->getQueueLength();
 * $obj->read(10);
 * $obj->get(8);
 */
class memcacheQueue{
 public static $client;   //memcache客户端连接
 public   $access;   //队列是否可更新
 private   $expire;   //过期时间,秒,1~2592000,即30天内
 private   $sleepTime;   //等待解锁时间,微秒
 private   $queueName;   //队列名称,唯一值
 private   $retryNum;   //重试次数,= 10 * 理论并发数
 public   $currentHead;  //当前队首值
 public   $currentTail;  //当前队尾值

 const MAXNUM  = 20000;    //最大队列数,建议上限10K
 const HEAD_KEY = '_lkkQueueHead_';  //队列首kye
 const TAIL_KEY = '_lkkQueueTail_';  //队列尾key
 const VALU_KEY = '_lkkQueueValu_';  //队列值key
 const LOCK_KEY = '_lkkQueueLock_';  //队列锁key

    /**
     * 构造函数
     * @param string $queueName  队列名称
     * @param int $expire 过期时间
     * @param array $config  memcache配置
     *
     * @return <type>
     */
 public function __construct($queueName ='',$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) ? 3600 : intval($expire)+1;
  $this->expire = $expire;
  $this->queueName = $queueName;
  $this->retryNum = 1000;

  $this->head_key = $this->queueName . self::HEAD_KEY;
  $this->tail_key = $this->queueName . self::TAIL_KEY;
  $this->lock_key = $this->queueName . self::LOCK_KEY;

  $this->_initSetHeadNTail();
 }

    /**
     * 初始化设置队列首尾值
     */
 private function _initSetHeadNTail(){
  //当前队列首的数值
  $this->currentHead = memcache_get(self::$client, $this->head_key);
  if($this->currentHead === false) $this->currentHead =0;

  //当前队列尾的数值
  $this->currentTail = memcache_get(self::$client, $this->tail_key);
  if($this->currentTail === false) $this->currentTail =0;
 }

    /**
     * 当取出元素时,改变队列首的数值
  * @param int $step 步长值
     */
 private function _changeHead($step=1){
  $this->currentHead += $step;
  memcache_set(self::$client, $this->head_key,$this->currentHead,false,$this->expire);
 }

    /**
     * 当添加元素时,改变队列尾的数值
  * @param int $step 步长值
     * @param bool $reverse 是否反向
     * @return null
     */
 private function _changeTail($step=1, $reverse =false){
  if(!$reverse){
   $this->currentTail += $step;
  }else{
   $this->currentTail -= $step;
  }

  memcache_set(self::$client, $this->tail_key,$this->currentTail,false,$this->expire);
 }

    /**
     * 队列是否为空
     * @return bool
     */
 private function _isEmpty(){
  return (bool)($this->currentHead === $this->currentTail);
 }

    /**
     * 队列是否已满
     * @return bool
     */
 private function _isFull(){
  $len = $this->currentTail - $this->currentHead;
  return (bool)($len === self::MAXNUM);
 }

    /**
     * 队列加锁
     */
 private function _getLock(){
  if($this->access === false){
   while(!memcache_add(self::$client, $this->lock_key, 1, false, $this->expire) ){
    usleep($this->sleepTime);
    @$i++;
    if($i > $this->retryNum){//尝试等待N次
     return false;
     break;
    }
   }

   $this->_initSetHeadNTail();
   return $this->access = true;
  }

  return $this->access;
 }

    /**
     * 队列解锁
     */
 private function _unLock(){
  memcache_delete(self::$client, $this->lock_key, 0);
  $this->access = false;
 }

    /**
     * 获取当前队列的长度
     * 该长度为理论长度,某些元素由于过期失效而丢失,真实长度<=该长度
     * @return int
     */
 public function getQueueLength(){
  $this->_initSetHeadNTail();
  return intval($this->currentTail - $this->currentHead);
 }

    /**
     * 添加队列数据
     * @param void $data 要添加的数据
     * @return bool
     */
 public function add($data){
  if(!$this->_getLock()) return false;

  if($this->_isFull()){
   $this->_unLock();
   return false;
  }

  $value_key = $this->queueName . self::VALU_KEY . strval($this->currentTail +1);
  $result = memcache_set(self::$client, $value_key, $data, MEMCACHE_COMPRESSED, $this->expire);
  if($result){
   $this->_changeTail();
  }

  $this->_unLock();
  return $result;
 }

    /**
     * 读取队列数据
     * @param int $length 要读取的长度(反向读取使用负数)
     * @return array
     */
 public function read($length=0){
  if(!is_numeric($length)) return false;
  $this->_initSetHeadNTail();

  if($this->_isEmpty()){
   return false;
  }

  if(empty($length)) $length = self::MAXNUM;//默认所有
  $keyArr = array();
  if($length >0){//正向读取(从队列首向队列尾)
   $tmpMin = $this->currentHead;
   $tmpMax = $tmpMin + $length;
   for($i= $tmpMin; $i<=$tmpMax; $i++){
    $keyArr[] = $this->queueName . self::VALU_KEY . $i;
   }
  }else{//反向读取(从队列尾向队列首)
   $tmpMax = $this->currentTail;
   $tmpMin = $tmpMax + $length;
   for($i= $tmpMax; $i >$tmpMin; $i--){
    $keyArr[] = $this->queueName . self::VALU_KEY . $i;
   }
  }

  $result = @memcache_get(self::$client, $keyArr);

  return $result;
 }

    /**
     * 取出队列数据
     * @param int $length 要取出的长度(反向读取使用负数)
     * @return array
     */
 public function get($length=0){
  if(!is_numeric($length)) return false;
  if(!$this->_getLock()) return false;

  if($this->_isEmpty()){
   $this->_unLock();
   return false;
  }

  if(empty($length)) $length = self::MAXNUM;//默认所有
  $length = intval($length);
  $keyArr = array();
  if($length >0){//正向读取(从队列首向队列尾)
   $tmpMin = $this->currentHead;
   $tmpMax = $tmpMin + $length;
   for($i= $tmpMin; $i<=$tmpMax; $i++){
    $keyArr[] = $this->queueName . self::VALU_KEY . $i;
   }
   $this->_changeHead($length);
  }else{//反向读取(从队列尾向队列首)
   $tmpMax = $this->currentTail;
   $tmpMin = $tmpMax + $length;
   for($i= $tmpMax; $i >$tmpMin; $i--){
    $keyArr[] = $this->queueName . self::VALU_KEY . $i;
   }
   $this->_changeTail(abs($length), true);
  }
  $result = @memcache_get(self::$client, $keyArr);

  foreach($keyArr as $v){//取出之后删除
   @memcache_delete(self::$client, $v, 0);
  }

  $this->_unLock();

  return $result;
 }

    /**
     * 清空队列
     */
 public function clear(){
  if(!$this->_getLock()) return false;

  if($this->_isEmpty()){
   $this->_unLock();
   return false;
  }

  $tmpMin = $this->currentHead--;
  $tmpMax = $this->currentTail++;

  for($i= $tmpMin; $i<=$tmpMax; $i++){
   $tmpKey = $this->queueName . self::VALU_KEY . $i;
   @memcache_delete(self::$client, $tmpKey, 0);
  }

  $this->currentTail = $this->currentHead = 0;
  memcache_set(self::$client, $this->head_key,$this->currentHead,false,$this->expire);
  memcache_set(self::$client, $this->tail_key,$this->currentTail,false,$this->expire);

  $this->_unLock();
 }

 /*
  * 清除所有memcache缓存数据
  */
 public function memFlush(){
  memcache_flush(self::$client);
 }

}//end class

时间: 2024-08-08 02:38:19

php的memcache类分享(memcache队列)_php实例的相关文章

php的memcache类分享

 这篇文章主要介绍了php的memcache类的使用方法(memcache队列),需要的朋友可以参考下 memcacheQueue.class.php   代码如下: <?php /**  * PHP memcache 队列类  * @author LKK/lianq.net  * @version 0.3  * @修改说明:  * 1.放弃了之前的AB面轮值思路,使用类似数组的构造,重写了此类.  * 2.队列默认先进先出,但增加了反向读取功能.  * 3.感谢网友FoxHunter提出的宝贵

Joomla语言翻译类Jtext用法分析_php实例

本文实例讲述了Joomla语言翻译类Jtext用法.分享给大家供大家参考,具体如下: 基本使用方法: Jtext是Joomla中实现多语言翻译的一个对象,最基本的使用方法如下: Jtext::_('LANGUAGE CODE'); 如果是作为变量的话,则直接使用,如: $var = Jtext::_('LANGUAGE CODE'); 如果需要显示出来,可以用echo的命令让它显示,这种方式在模板文件中是最常见的,如: echo Jtext::_('LANGUAGE CODE'); 语言包文件:

php Memcache 中实现消息队列_php技巧

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

PHP环境中Memcache的安装和使用_php实例

Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable.Memcache官方网站:http://www.danga.com/memcached,更多详细的信息可以来这里了解. 为什么会有Memcache和memcached两

Yii实现自动加载类地图的方法_php实例

本文实例讲述了Yii实现自动加载类地图的方法.分享给大家供大家参考.具体如下: Yii继承的一个静态属性$classMap,可以用于Yii的自动加载类地图.数组中的键是类名,数组中的值是相应类文件的路径. require_once($yii); $app = Yii::createWebApplication($config); Yii::$classMap = array( 'JPhpMailer' => Yii::getPathOfAlias('ext') . '/phpmailer/JPh

Zend Framework教程之动作的基类Zend_Controller_Action详解_php实例

本文实例讲述了Zend Framework教程之动作的基类Zend_Controller_Action.分享给大家供大家参考,具体如下: Zend_Controller_Action的实现 Zend Framework的动作控制器需要继承Zend_Controller_Action,Zend_Controller_Action提供了动作控制器的基本功能,具体参考如下代码: Zend_Controller_Action_Interface <?php interface Zend_Controll

PHP实现的汉字拼音转换和公历农历转换类及使用示例_php实例

本文整理了PHP汉字拼音转换和公历农历转换两个功能类文件,非常实用.比如我们查找通讯录可以通过联系人姓名的拼音首字母来查询,可以通过首字母来导航大数据量,可以通过转换拼音来做网站优化等.公农历转化一般用在日历日程安排的项目中,方便农历的节日提醒等等. 1.PHP汉字转拼音 Pinyin.class.php类文件可以将大多数汉字转换成汉语拼音,当然也有个别生僻字不能转换,如果你想转换所有的汉字拼音的话,可能需要再配合一个汉字字库来实现,使用该类文件就基本能满足你的项目需求了.用法: 复制代码 代码

CI框架扩展系统核心类的方法分析_php实例

本文实例讲述了CI框架扩展系统核心类的方法.分享给大家供大家参考,具体如下: 首先你系统扩展类是放在application/core下面的,本来系统核心类是CI_Controller,所以你不能以CI_开头了,你需要打开 application/config/config.php 修改: $config['subclass_prefix'] = 'MY_'; 为你的前缀! 一些公共的模块就可以卸载自己的核心类里面了! 这几天读了Dilicms(轻量级的后台架构),比如说他的后台扩展类是这样的:

Zend Framework分页类用法详解_php实例

本文实例讲述了Zend Framework分页类用法.分享给大家供大家参考,具体如下: 1.分页类Pagination.php,最好是把这个类放在Zend目录下 class XY_Pagination { private $_navigationItemCount = 10; //导航栏显示导航总页数 private $_pageSize = null; //每页项目数 private $_align = "right"; //导航栏显示位置 private $_itemCount =