下面類對memcache進行封裝,包括了對memcache的添加,讀取,清空,刪除,擷取伺服器的資訊,快取服務池等。
/******************************************* * 檔案名稱: /includes/memcache.class.php * 功能: memcache 緩衝類 * 版本: 1.0 * 日期: 2016-07-16 * 程式名: memcache快取作業類 -----(PHP中需載入memcache擴充) * 作者: JoeXiong * 著作權: Copyright@2016-2016 github.com/JoeXiong All Rights Reserved *********************************************/classjoememcache{privatestatic$_instance; private$_memcache; private$_which = 0; private$_memservers; publicstaticfunctiongetInstance() {if (! (self::$_instanceinstanceof joememcache)) { self::$_instance = newself(); } return is_object(self::$_instance->_memcache) ? self::$_instance : false; } privatefunction__construct() {if (extension_loaded('memcache')) { $this->_memcache = new Memcache(); $this->_which = 1; } elseif (extension_loaded('memcached')) { $this->_memcache = new Memcached(); $this->_which = 2; } else$this->_memcache = FALSE; } /** * 儲存緩衝 * @param unknown $key * @param unknown $data * @param number $ttl * @param string $isCompress * @return boolean */publicfunctionSave($key, $data, $ttl = 60,$isCompress = FALSE){if($this->_which == 1) return$this->_memcache->set($key, array($data, time(), $ttl), !$isCompress ? 0 : MEMCACHE_COMPRESSED, $ttl);//使用time() 函數最新elseif($this->_which == 2) return$this->_memcache->set($key, array($data, time(), $ttl), $ttl); elsereturnFALSE; } /** * 讀取緩衝資訊 * @param unknown $key * @return Ambigous |boolean */publicfunctionreadMetaData($key){$value = $this->_memcache->get($key); if(is_array($value) && count($value) == 3){ list($data, $time, $ttl) = $value; return (time() < $time + $ttl) ? $data : array(); } else { returnfalse; } } /** * @deprecated 讀取多個緩衝資訊 * @param array $array * @return 成功$value(array), 失敗FALSE(bool) */publicfunctionreadMultiData($keys) {if(!is_array($keys)) { returnFALSE; } $rtn = array(); if($this->_which == 1) $rtn = $this->_memcache->get($keys); elseif($this->_which == 2) $rtn = $this->_memcache->getMulti($keys); $now = time(); foreach($rtnas$key=>&$v) { if(!empty($v)) { list($data, $time, $ttl) = $v; $v = ($now < $time + $ttl) ? $data : array(); } } return$rtn; } /** * @description 讀取緩衝 * @param $key 查詢索引key * @return 成功 array 失敗 FALSE */publicfunctionRead($key) {$data = $this->_memcache->get($key); return is_array($data) ? $data[0] : FALSE; } /** * @description 刪除緩衝 * @param $key 將要刪除的key * return bool 成功 TRUE 失敗 FALSE */publicfunctionDelete($key) {return$this->_memcache->delete($key); } /** * @description 清空所有緩衝 * @return bool true or false */publicfunctionClear() {return$this->_memcache->flush(); } /** * 擷取快取服務器池中所有伺服器統計資訊 * @return array */publicfunctiongetExtendedStats() {//return $this->_memcache->getExtendedStats();if($this->_which == 1) { return$this->_memcache->getExtendedStats(); } elseif($this->_which == 2) return$this->_memcache->getServerList(); elsereturnFALSE; } /** * 快取服務器池 */publicfunctionaddServer() {foreach ($this->_memservers as$h) { $this->_memcache->addServer($h['host'], isset($h['port']) ? $h['port'] : 11211); // 預設連接埠為11211 } } /** * 擷取memecache伺服器位址 */publicfunctiongetHost() {return$this->_memservers; } /** * 設定memcache伺服器位址 */publicfunctionsetHost(array $servers) {if (is_array($servers) && ! empty($servers)) { $this->_memservers = $servers; $this->addServer(); } } privatefunction__clone() {}}
以下是對封裝類的使用方法
//初始化memcache$memcache = joememcache::getInstance(); //是否開啟memcache擴充if($memcache){ //memcache的配置參數,放在設定檔中global$memServer,$memKey; //設定memecache伺服器位址$memcache->setHost($memServer); //通過key讀取資料$news = $memcache->readMetaData($memKey['sharetypekey']['10001']['Key'].$id); //memcache沒有當前key的資料,則從資料庫查詢並儲存到memecache伺服器中if(!$news){ $news = $this->getCurSharenewsInfo($id); $memcache->Save($memKey['sharetypekey']['10001']['Key'].$id,$news,$memKey['sharetypekey']['10001']['Time']); } }else{ //直接資料庫查詢資料$news = $this->getCurSharenewsInfo($id); }
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了 PHP-Mmecache操作類詳細介紹,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。