目录
memcacheQueue.class.php" >memcacheQueue.class.php
首页 后端开发 php教程 php自各儿实现memcached的队列类

php自各儿实现memcached的队列类

Jun 13, 2016 pm 12:19 PM
gt memcache return this

php自己实现memcached的队列类

PHP的memcache队列类
version 0.2
1.修改了changeHead方法,当get(1)只取一条数据时$head_key的值没改变的问题
2.修改了clear方法,当队列较小时按最大队列长度删除的问题

测试结果
队列类添加20000条数据,用时10.390461921692秒
队列类读取20000条数据,用时0.087434053421021秒
队列类取出20000条数据,用时0.91940212249756秒
队列类清空20000条数据,用时0.00077390670776367秒
------------------
PHP扩展添加20000条数据,用时0.9195499420166秒
PHP扩展读取20000条数据,用时0.050693988800049秒
PHP扩展取出20000条数据,用时0.88281202316284秒
PHP扩展删除20000条数据,用时0.81996202468872秒
------------------
队列写入的性能是个问题,待改善
[email protected]

memcacheQueue.class.php

<?php /* * memcache队列类 * 支持多进程并发写入、读取 * 边写边读,AB面轮值替换 * @author lkk/blog.lianq.net * @version 0.2 * @create on 9:25 2012-9-28 * * @edited on 14:03 2013-4-28 * 修改说明: *      1.修改了changeHead方法,当get(1)只取一条数据时$head_key的值没改变的问题 *      2.修改了clear方法,当队列较小时按最大队列长度删除的问题 * * 使用方法: *      $obj = new memcacheQueue(&#39;duilie&#39;); *      $obj->add('1asdf'); *      $obj->getQueueLength(); *      $obj->read(11); *      $obj->get(8); */ class memcacheQueue{    public static   $client;            //memcache客户端连接    public          $access;            //队列是否可更新       private         $currentSide;       //当前轮值的队列面:A/B    private         $lastSide;          //上一轮值的队列面:A/B    private         $sideAHead;         //A面队首值    private         $sideATail;         //A面队尾值    private         $sideBHead;         //B面队首值    private         $sideBTail;         //B面队尾值    private         $currentHead;       //当前队首值    private         $currentTail;       //当前队尾值    private         $lastHead;          //上轮队首值    private         $lastTail;          //上轮队尾值     private         $expire;            //过期时间,秒,1~2592000,即30天内    private         $sleepTime;         //等待解锁时间,微秒    private         $queueName;         //队列名称,唯一值    private         $retryNum;          //重试次数,= 10 * 理论并发数         const   MAXNUM      = 10000;                //(单面)最大队列数,建议上限10K    const   HEAD_KEY    = '_lkkQueueHead_';     //队列首kye    const   TAIL_KEY    = '_lkkQueueTail_';     //队列尾key    const   VALU_KEY    = '_lkkQueueValu_';     //队列值key    const   LOCK_KEY    = '_lkkQueueLock_';     //队列锁key    const   SIDE_KEY    = '_lkkQueueSide_';     //轮值面key         /*     * 构造函数     * @param   [queueName] string  队列名称     * @param   [expire]    string  过期时间     * @param   [config]    array   memcache服务器参数         * @return  NULL     */    public function __construct($queueName ='',$expire='',$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 : (int)$expire+1;        $this->expire = $expire;        $this->queueName = $queueName;        $this->retryNum = 20000;                 $side = memcache_add(self::$client, $queueName . self::SIDE_KEY, 'A',false, $expire);        $this->getHeadNTail($queueName);        if(!isset($this->sideAHead) || empty($this->sideAHead)) $this->sideAHead = 0;        if(!isset($this->sideATail) || empty($this->sideATail)) $this->sideATail = 0;        if(!isset($this->sideBHead) || empty($this->sideBHead)) $this->sideBHead = 0;        if(!isset($this->sideBTail) || empty($this->sideBTail)) $this->sideBTail = 0;    }         /*     * 获取队列首尾值     * @param   [queueName] string  队列名称     * @return  NULL     */    private function getHeadNTail($queueName){        $this->sideAHead = (int)memcache_get(self::$client, $queueName.'A'. self::HEAD_KEY);        $this->sideATail = (int)memcache_get(self::$client, $queueName.'A'. self::TAIL_KEY);        $this->sideBHead = (int)memcache_get(self::$client, $queueName.'B'. self::HEAD_KEY);        $this->sideBTail = (int)memcache_get(self::$client, $queueName.'B'. self::TAIL_KEY);    }         /*     * 获取当前轮值的队列面     * @return  string  队列面名称     */    public function getCurrentSide(){        $currentSide = memcache_get(self::$client, $this->queueName . self::SIDE_KEY);        if($currentSide == 'A'){            $this->currentSide = 'A';            $this->lastSide = 'B';                $this->currentHead   = $this->sideAHead;            $this->currentTail   = $this->sideATail;            $this->lastHead      = $this->sideBHead;            $this->lastTail      = $this->sideBTail;                  }else{            $this->currentSide = 'B';            $this->lastSide = 'A';             $this->currentHead   = $this->sideBHead;            $this->currentTail   = $this->sideBTail;            $this->lastHead      = $this->sideAHead;            $this->lastTail      = $this->sideATail;                              }                 return $this->currentSide;    }         /*     * 队列加锁     * @return boolean     */    private function getLock(){        if($this->access === false){            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;    }         /*     * 队列解锁     * @return NULL     */    private function unLock(){        memcache_delete(self::$client, $this->queueName .self::LOCK_KEY);        $this->access = false;    }         /*     * 添加数据     * @param   [data]  要存储的值     * @return  boolean     */    public function add($data=''){        $result = false;        if(empty($data)) return $result;        if(!$this->getLock()){            return $result;        }         $this->getHeadNTail($this->queueName);        $this->getCurrentSide();                 if($this->isFull()){            $this->unLock();            return false;        }                 if($this->currentTail queueName .$this->currentSide . self::VALU_KEY . $this->currentTail;            if(memcache_set(self::$client, $value_key, $data, false, $this->expire)){                $this->changeTail();                $result = true;            }        }else{//当前队列已满,更换轮值面            $this->unLock();            $this->changeCurrentSide();            return $this->add($data);        }         $this->unLock();        return $result;    }         /*     * 取出数据     * @param   [length]    int 数据的长度     * @return  array     */    public function get($length=0){        if(!is_numeric($length)) return false;        if(empty($length)) $length = self::MAXNUM * 2;//默认读取所有        if(!$this->getLock()) return false;         if($this->isEmpty()){            $this->unLock();            return false;        }                 $keyArray   = $this->getKeyArray($length);        $lastKey    = $keyArray['lastKey'];        $currentKey = $keyArray['currentKey'];        $keys       = $keyArray['keys'];        $this->changeHead($this->lastSide,$lastKey);        $this->changeHead($this->currentSide,$currentKey);                 $data   = @memcache_get(self::$client, $keys);        if(empty($data)) $data = array();        foreach($keys as $v){//取出之后删除            @memcache_delete(self::$client, $v, 0);        }        $this->unLock();         return $data;    }         /*     * 读取数据     * @param   [length]    int 数据的长度     * @return  array     */    public function read($length=0){        if(!is_numeric($length)) return false;        if(empty($length)) $length = self::MAXNUM * 2;//默认读取所有        $keyArray   = $this->getKeyArray($length);        $data   = @memcache_get(self::$client, $keyArray['keys']);        if(empty($data)) $data = array();        return $data;    }         /*     * 获取队列某段长度的key数组     * @param   [length]    int 队列长度     * @return  array     */    private function getKeyArray($length){        $result = array('keys'=>array(),'lastKey'=>null,'currentKey'=>null);        $this->getHeadNTail($this->queueName);        $this->getCurrentSide();        if(empty($length)) return $result;                 //先取上一面的key        $i = $result['lastKey'] = 0;        for($i=0;$ilastHead + $i;            if($result['lastKey'] >= $this->lastTail) break;            $result['keys'][] = $this->queueName .$this->lastSide . self::VALU_KEY . $result['lastKey'];        }                 //再取当前面的key        $j = $length - $i;        $k = $result['currentKey'] = 0;        for($k=0;$kcurrentHead + $k;            if($result['currentKey'] >= $this->currentTail) break;            $result['keys'][] = $this->queueName .$this->currentSide . self::VALU_KEY . $result['currentKey'];        }         return $result;    }         /*     * 更新当前轮值面队列尾的值     * @return  NULL     */    private function changeTail(){        $tail_key = $this->queueName .$this->currentSide . self::TAIL_KEY;        memcache_add(self::$client, $tail_key, 0,false, $this->expire);//如果没有,则插入;有则false;        $v = memcache_get(self::$client, $tail_key) +1;        memcache_set(self::$client, $tail_key,$v,false,$this->expire);    }         /*     * 更新队列首的值     * @param   [side]      string  要更新的面     * @param   [headValue] int     队列首的值     * @return  NULL     */    private function changeHead($side,$headValue){        $head_key = $this->queueName .$side . self::HEAD_KEY;        $tail_key = $this->queueName .$side . self::TAIL_KEY;        $sideTail = memcache_get(self::$client, $tail_key);        if($headValue expire);        }elseif($headValue >= $sideTail){            $this->resetSide($side);        }    }         /*     * 重置队列面,即将该队列面的队首、队尾值置为0     * @param   [side]  string  要重置的面     * @return  NULL     */    private function resetSide($side){        $head_key = $this->queueName .$side . self::HEAD_KEY;        $tail_key = $this->queueName .$side . self::TAIL_KEY;        memcache_set(self::$client, $head_key,0,false,$this->expire);        memcache_set(self::$client, $tail_key,0,false,$this->expire);    }              /*     * 改变当前轮值队列面     * @return  string     */    private function changeCurrentSide(){        $currentSide = memcache_get(self::$client, $this->queueName . self::SIDE_KEY);        if($currentSide == 'A'){            memcache_set(self::$client, $this->queueName . self::SIDE_KEY,'B',false,$this->expire);            $this->currentSide = 'B';        }else{            memcache_set(self::$client, $this->queueName . self::SIDE_KEY,'A',false,$this->expire);            $this->currentSide = 'A';        }        return $this->currentSide;    }         /*     * 检查当前队列是否已满     * @return  boolean     */    public function isFull(){        $result = false;        if($this->sideATail == self::MAXNUM && $this->sideBTail == self::MAXNUM){            $result = true;        }        return $result;    }         /*     * 检查当前队列是否为空     * @return  boolean     */    public function isEmpty(){        $result = true;        if($this->sideATail > 0 || $this->sideBTail > 0){            $result = false;        }        return $result;    }         /*     * 获取当前队列的长度     * 该长度为理论长度,某些元素由于过期失效而丢失,真实长度小于或等于该长度     * @return  int     */    public function getQueueLength(){        $this->getHeadNTail($this->queueName);         $sideALength = $this->sideATail - $this->sideAHead;        $sideBLength = $this->sideBTail - $this->sideBHead;        $result = $sideALength + $sideBLength;                 return $result;    }          /*     * 清空当前队列数据,仅保留HEAD_KEY、TAIL_KEY、SIDE_KEY三个key     * @return  boolean     */    public function clear(){        if(!$this->getLock()) return false;        $this->getHeadNTail($this->queueName);        $AHead = $this->sideAHead;$AHead--;        $ATail = $this->sideATail;$ATail++;        $BHead = $this->sideBHead;$BHead--;              $BTail = $this->sideBTail;$BTail++;                 //删除A面        for($i=$AHead;$iqueueName.'A'. self::VALU_KEY .$i, 0);        }                 //删除B面        for($j=$BHead;$jqueueName.'A'. self::VALU_KEY .$j, 0);        }         $this->unLock();        $this->resetSide('A');        $this->resetSide('B');        return true;    }         /*     * 清除所有memcache缓存数据     * @return  NULL     */    public function memFlush(){        memcache_flush(self::$client);    }   }
登录后复制

test.php

<?php //memcacheQueue队列类测试header("Content-type: text/html; charset=utf-8;private, must-revalidate");require_once(&#39;memcacheQueue.class.php&#39;);//获取时间,精度微秒function microtimeFloat(){    list($usec, $sec) = explode(" ", microtime());    return ((float)$usec + (float)$sec);}  $queObj = new memcacheQueue(&#39;duilie&#39;);$client = memcache_pconnect(&#39;127.0.0.1&#39;,11211);$length = 20000;  /*------------------------------------------ * 队列类测试 *------------------------------------------ */ //写数据$time_add_start = microtimeFloat();for($i=1;$i<=$length;$i++){    $queObj->add($i);}$time_add_end = microtimeFloat();$count = $queObj->getQueueLength();$time_use = $time_add_end - $time_add_start;echo "队列类添加{$count}条数据,用时{$time_use}秒<br>"; //读数据$time_read_start = microtimeFloat();$tmpArr = $queObj->read();$time_read_end = microtimeFloat();$count = $queObj->getQueueLength();$time_use = $time_read_end - $time_read_start;echo "队列类读取{$count}条数据,用时{$time_use}秒<br>"; //取出数据$time_get_start = microtimeFloat();$tmpArr = $queObj->get();$time_get_end = microtimeFloat();$count = count($tmpArr);$time_use = $time_get_end - $time_get_start;echo "队列类取出{$count}条数据,用时{$time_use}秒<br>"; //清空队列$time_clear_start = microtimeFloat();$queObj->clear();$time_clear_end = microtimeFloat();$time_use = $time_clear_end - $time_clear_start;echo "队列类清空{$count}条数据,用时{$time_use}秒<br>"; echo "<hr><br>"; /*------------------------------------------ * PHP memcache扩展测试 *------------------------------------------ */ //写数据$time_add_start = microtimeFloat();for($i=1;$i"; //读数据$time_read_start = microtimeFloat();$keyArr = array();for($i=1;$i"; //取出数据//读数据$time_get_start = microtimeFloat();$keyArr = array();for($i=1;$i";$time_use2 = $time_get_end2 - $time_get_end1;echo "PHP扩展删除{$coutn}条数据,用时{$time_use2}秒<br>";
登录后复制


本文来自:http://www.oschina.net/code/snippet_111731_20890

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

华为GT3 Pro和GT4的差异是什么? 华为GT3 Pro和GT4的差异是什么? Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

C语言return的用法详解 C语言return的用法详解 Oct 07, 2023 am 10:58 AM

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

如何使用PHP开发中的Memcache? 如何使用PHP开发中的Memcache? Nov 07, 2023 pm 12:49 PM

在Web开发中,我们经常需要使用缓存技术来提高网站的性能和响应速度。Memcache是一种流行的缓存技术,它可以缓存任何数据类型、支持高并发和高可用性。本文将介绍如何使用PHP开发中的Memcache,并提供具体代码示例。一、安装Memcache要使用Memcache,我们首先需要在服务器上安装Memcache扩展。在CentOS操作系统中,可以使用以下命令

修复:截图工具在 Windows 11 中不起作用 修复:截图工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

如何修复无法连接到iPhone上的App Store错误 如何修复无法连接到iPhone上的App Store错误 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

PHP开发中如何使用Memcache进行高效的数据读写操作? PHP开发中如何使用Memcache进行高效的数据读写操作? Nov 07, 2023 pm 03:48 PM

在PHP开发中,使用Memcache缓存系统可以大大提高数据读写的效率。Memcache是一种基于内存的缓存系统,它可以将数据缓存在内存中,避免频繁的读写数据库。本文将介绍如何在PHP中使用Memcache进行高效的数据读写操作,并提供具体的代码示例。一、安装和配置Memcache首先,需要在服务器上安装Memcache扩展。可以通过

PHP开发中如何使用Memcache进行高效的数据写入和查询? PHP开发中如何使用Memcache进行高效的数据写入和查询? Nov 07, 2023 pm 01:36 PM

PHP开发中如何使用Memcache进行高效的数据写入和查询?随着互联网应用的不断发展,对于系统性能的要求越来越高。在PHP开发中,为了提高系统的性能和响应速度,我们经常会使用各种缓存技术。而其中一个常用的缓存技术就是Memcache。Memcache是一种高性能的分布式内存对象缓存系统,可以用来缓存数据库查询结果、页面片段、会话数据等。通过将数据存储在内存

使用JavaScript中return关键字 使用JavaScript中return关键字 Feb 18, 2024 pm 12:45 PM

JavaScript中return的用法,需要具体代码示例在JavaScript中,return语句用于指定从函数中返回的值。它不仅可以用于结束函数的执行,还可以将一个值返回给调用函数的地方。return语句有以下几个常见的用法:返回一个值return语句可以用来返回一个值给调用函数的地方。下面是一个简单的示例:functionadd(a,b){

See all articles