php的memcache類(lèi)分享(memcache隊(duì)列)
memcacheQueue.class.php
<?php
/**
* PHP memcache 隊(duì)列類(lèi)
* @author LKK/lianq.net
* @version 0.3
* @修改說(shuō)明:
* 1.放棄了之前的AB面輪值思路,使用類(lèi)似數(shù)組的構(gòu)造,重寫(xiě)了此類(lèi).
* 2.隊(duì)列默認(rèn)先進(jìn)先出,但增加了反向讀取功能.
* 3.感謝網(wǎng)友FoxHunter提出的寶貴意見(jiàn).
* @example:
* $obj = new memcacheQueue('duilie');
* $obj->add('1asdf');
* $obj->getQueueLength();
* $obj->read(10);
* $obj->get(8);
*/
class memcacheQueue{
public static $client; //memcache客戶(hù)端連接
public $access; //隊(duì)列是否可更新
private $expire; //過(guò)期時(shí)間,秒,1~2592000,即30天內(nèi)
private $sleepTime; //等待解鎖時(shí)間,微秒
private $queueName; //隊(duì)列名稱(chēng),唯一值
private $retryNum; //重試次數(shù),= 10 * 理論并發(fā)數(shù)
public $currentHead; //當(dāng)前隊(duì)首值
public $currentTail; //當(dāng)前隊(duì)尾值
const MAXNUM = 20000; //最大隊(duì)列數(shù),建議上限10K
const HEAD_KEY = '_lkkQueueHead_'; //隊(duì)列首kye
const TAIL_KEY = '_lkkQueueTail_'; //隊(duì)列尾key
const VALU_KEY = '_lkkQueueValu_'; //隊(duì)列值key
const LOCK_KEY = '_lkkQueueLock_'; //隊(duì)列鎖key
/**
* 構(gòu)造函數(shù)
* @param string $queueName 隊(duì)列名稱(chēng)
* @param int $expire 過(guò)期時(shí)間
* @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);//當(dāng)客戶(hù)斷開(kāi)連接,允許繼續(xù)執(zhí)行
set_time_limit(0);//取消腳本執(zhí)行延時(shí)上限
$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();
}
/**
* 初始化設(shè)置隊(duì)列首尾值
*/
private function _initSetHeadNTail(){
//當(dāng)前隊(duì)列首的數(shù)值
$this->currentHead = memcache_get(self::$client, $this->head_key);
if($this->currentHead === false) $this->currentHead =0;
//當(dāng)前隊(duì)列尾的數(shù)值
$this->currentTail = memcache_get(self::$client, $this->tail_key);
if($this->currentTail === false) $this->currentTail =0;
}
/**
* 當(dāng)取出元素時(shí),改變隊(duì)列首的數(shù)值
* @param int $step 步長(zhǎng)值
*/
private function _changeHead($step=1){
$this->currentHead += $step;
memcache_set(self::$client, $this->head_key,$this->currentHead,false,$this->expire);
}
/**
* 當(dāng)添加元素時(shí),改變隊(duì)列尾的數(shù)值
* @param int $step 步長(zhǎng)值
* @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);
}
/**
* 隊(duì)列是否為空
* @return bool
*/
private function _isEmpty(){
return (bool)($this->currentHead === $this->currentTail);
}
/**
* 隊(duì)列是否已滿(mǎn)
* @return bool
*/
private function _isFull(){
$len = $this->currentTail - $this->currentHead;
return (bool)($len === self::MAXNUM);
}
/**
* 隊(duì)列加鎖
*/
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;
}
/**
* 隊(duì)列解鎖
*/
private function _unLock(){
memcache_delete(self::$client, $this->lock_key, 0);
$this->access = false;
}
/**
* 獲取當(dāng)前隊(duì)列的長(zhǎng)度
* 該長(zhǎng)度為理論長(zhǎng)度,某些元素由于過(guò)期失效而丟失,真實(shí)長(zhǎng)度<=該長(zhǎng)度
* @return int
*/
public function getQueueLength(){
$this->_initSetHeadNTail();
return intval($this->currentTail - $this->currentHead);
}
/**
* 添加隊(duì)列數(shù)據(jù)
* @param void $data 要添加的數(shù)據(jù)
* @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;
}
/**
* 讀取隊(duì)列數(shù)據(jù)
* @param int $length 要讀取的長(zhǎng)度(反向讀取使用負(fù)數(shù))
* @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;//默認(rèn)所有
$keyArr = array();
if($length >0){//正向讀取(從隊(duì)列首向隊(duì)列尾)
$tmpMin = $this->currentHead;
$tmpMax = $tmpMin + $length;
for($i= $tmpMin; $i<=$tmpMax; $i++){
$keyArr[] = $this->queueName . self::VALU_KEY . $i;
}
}else{//反向讀取(從隊(duì)列尾向隊(duì)列首)
$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;
}
/**
* 取出隊(duì)列數(shù)據(jù)
* @param int $length 要取出的長(zhǎng)度(反向讀取使用負(fù)數(shù))
* @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;//默認(rèn)所有
$length = intval($length);
$keyArr = array();
if($length >0){//正向讀取(從隊(duì)列首向隊(duì)列尾)
$tmpMin = $this->currentHead;
$tmpMax = $tmpMin + $length;
for($i= $tmpMin; $i<=$tmpMax; $i++){
$keyArr[] = $this->queueName . self::VALU_KEY . $i;
}
$this->_changeHead($length);
}else{//反向讀取(從隊(duì)列尾向隊(duì)列首)
$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;
}
/**
* 清空隊(duì)列
*/
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緩存數(shù)據(jù)
*/
public function memFlush(){
memcache_flush(self::$client);
}
}//end class
- PHP+memcache實(shí)現(xiàn)消息隊(duì)列案例分享
- PHP下操作Linux消息隊(duì)列完成進(jìn)程間通信的方法
- php Memcache 中實(shí)現(xiàn)消息隊(duì)列
- PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類(lèi)實(shí)例
- PHP隊(duì)列用法實(shí)例
- php實(shí)現(xiàn)的雙向隊(duì)列類(lèi)實(shí)例
- php中使用redis隊(duì)列操作實(shí)例代碼
- 隊(duì)列在編程中的實(shí)際應(yīng)用(php)
- PHP消息隊(duì)列用法實(shí)例分析
相關(guān)文章
PHP中數(shù)據(jù)庫(kù)單例模式的實(shí)現(xiàn)代碼分享
這篇文章主要介紹了PHP中數(shù)據(jù)庫(kù)單例模式的實(shí)現(xiàn)代碼分享,本文先是講解了單例模式的一些知識(shí),然后給出了數(shù)據(jù)庫(kù)單例模式實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08
Laravel 實(shí)現(xiàn)添加多語(yǔ)言提示信息
今天小編就為大家分享一篇Laravel 實(shí)現(xiàn)添加多語(yǔ)言提示信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
CodeIgniter刪除和設(shè)置Cookie的方法
這篇文章主要介紹了CodeIgniter刪除和設(shè)置Cookie的方法,涉及CodeIgniter操作cookie的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
THINKPHP截取中文字符串函數(shù)實(shí)例代碼
在項(xiàng)目開(kāi)發(fā)中,我們常常會(huì)遇到英文、中文等字符串截取問(wèn)題,比如說(shuō)新聞列表頁(yè)面需要新聞內(nèi)容簡(jiǎn)介,這就要用到字符串截取了。2017-03-03
將PHP的session數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼實(shí)例
這里我們將分享兩個(gè)將PHP的session數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼實(shí)例,分別針對(duì)PostgreSQL與MySQL,需要的朋友可以參考下2016-06-06
PHP實(shí)現(xiàn)微信商戶(hù)支付企業(yè)付款到零錢(qián)功能
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)微信商戶(hù)支付企業(yè)付款到零錢(qián)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09

