溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php中memcache類的用法

發(fā)布時(shí)間:2021-07-27 18:52:24 來源:億速云 閱讀:93 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“php中memcache類的用法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php中memcache類的用法”吧!

memcacheQueue.class.php

復(fù)制代碼 代碼如下:


<?php
/**
 * PHP memcache 隊(duì)列類
 * @author LKK/lianq.net
 * @version 0.3
 * @修改說明:
 * 1.放棄了之前的AB面輪值思路,使用類似數(shù)組的構(gòu)造,重寫了此類.
 * 2.隊(duì)列默認(rèn)先進(jìn)先出,但增加了反向讀取功能.
 * 3.感謝網(wǎng)友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;   //隊(duì)列是否可更新
 private   $expire;   //過期時(shí)間,秒,1~2592000,即30天內(nèi)
 private   $sleepTime;   //等待解鎖時(shí)間,微秒
 private   $queueName;   //隊(duì)列名稱,唯一值
 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ì)列名稱
     * @param int $expire 過期時(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)客戶斷開連接,允許繼續(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 步長值
     */
 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 步長值
     * @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ì)列是否已滿
     * @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ì)列的長度
     * 該長度為理論長度,某些元素由于過期失效而丟失,真實(shí)長度<=該長度
     * @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 要讀取的長度(反向讀取使用負(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 要取出的長度(反向讀取使用負(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í)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI