搜尋
首頁後端開發php教程php的memcache类分享_PHP教程

php的memcache类分享_PHP教程

Jul 13, 2016 am 10:35 AM
memcachephp主要使用方法分享文章類別佇列

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

memcacheQueue.class.php   代码如下: 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;  }       /**      * 获取当前队列的长度      * 该长度为理论长度,某些元素由于过期失效而丢失,真实长度_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; $iqueueName . 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; $iqueueName . 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; $iqueueName . 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  

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/746591.htmlTechArticle这篇文章主要介绍了php的memcache类的使用方法(memcache队列),需要的朋友可以参考下 memcacheQueue.class.php代码如下:?php/*** PHP memcache 队列类* @aut...
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
簡單地說明PHP會話的概念。簡單地說明PHP會話的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

您如何循環中存儲在PHP會話中的所有值?您如何循環中存儲在PHP會話中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

說明如何使用會話進行用戶身份驗證。說明如何使用會話進行用戶身份驗證。Apr 26, 2025 am 12:04 AM

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

舉一個如何在PHP會話中存儲用戶名的示例。舉一個如何在PHP會話中存儲用戶名的示例。Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

哪些常見問題會導致PHP會話失敗?哪些常見問題會導致PHP會話失敗?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

您如何在PHP中調試與會話相關的問題?您如何在PHP中調試與會話相關的問題?Apr 25, 2025 am 12:12 AM

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

如果session_start()被多次調用會發生什麼?如果session_start()被多次調用會發生什麼?Apr 25, 2025 am 12:06 AM

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

您如何在PHP中配置會話壽命?您如何在PHP中配置會話壽命?Apr 25, 2025 am 12:05 AM

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

See all articles

熱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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具