热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php的memcache类分享(memcache队列)

这篇文章主要介绍了php的memcache类的使用方法(memcache队列),需要的朋友可以参考下

这篇文章主要介绍了php的memcache类的使用方法(memcache队列),需要的朋友可以参考下

memcacheQueue.class.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
*/
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


推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Matplotlib,带有已保 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
author-avatar
mobiledu2502881447
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有