在項目中,涉及大訪問量時,合理的使用緩存能減輕數據庫的壓力,同時提高用戶體驗。即在非實時性的需求的前提下,一小段時間內(若干秒),用於顯示的數據從緩存中獲取的,而不用直接讀取數據庫,能有效的減小數據庫的讀取壓力。這裏記錄一下php語言使用memcache的情形:php
首先,咱們創建一個memcachepool,能夠根據不一樣的配置讀取,生成不一樣的memcache實例。用到$memcache->addServer($host,$port,$flag);向鏈接池中添加一個memcache服務器。代碼示例以下數據庫
1 class memcachePool{ 2 private static $instance; 3 private $memcacheList = array(); 4 private function __construct(){ 5 6 } 7 public static function getInstance(){ 8 if(self::$instance != null) 9 return self::$instance; 10 self::$instance = new memcachePool(); 11 return self::$instance; 12 } 13 /** 14 * get memcache object from pool 15 * @param [type] $host 服務器 16 * @param [type] $port 端口 17 * @param [type] $flag 控制是否使用持久化鏈接。默認TRUE 18 * @return [type] 19 */ 20 public function getMemcache($host,$port,$flag){ 21 if(isset($this->memcacheList[$host.$port])) 22 return $this->memcacheList[$host.$port]; 23 24 $memcache = new Memcache(); 25 // 向鏈接池中添加一個memcache服務器 26 $memcache->addServer($host,$port,$flag); 27 //開啓大值自動壓縮,第一個參數表示處理數據大小的臨界點,第二個參數表示壓縮的比例,默認爲0.2 28 $memcache->setCompressThreshold(2000,0.2); 29 $this->memcacheList[$host.$port] = $memcache; 30 return $memcache; 31 } 32 }
接着實現一個包含memcache經常使用方法如add,set,get,flush,delete等的方法類,這裏命名爲dlufmemcachejson
1 class dlufMemcache{ 2 private $memcache = null; 3 function __construct($host,$port){ 4 5 $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true); 6 } 7 /** 8 * memcache set value 9 * @param [type] $key 鍵 10 * @param [type] $value 值 11 * @param integer $expire 到期的時間,若是此值設置爲0代表此數據永不過時 12 * @param integer $flag 標誌位 使用MEMCACHE_COMPRESSED指定對值進行壓縮(使用zlib) 13 * @param [type] $serializetype 14 */ 15 public function set($key,$value,$expire=0,$flag=0,$serializetype=null){ 16 if($serializetype == 'json' && is_array($value)){ 17 $value = json_encode($value); 18 } 19 $this->memcache->set($key,$value,$flag,$expire); 20 } 21 /** 22 * 從服務端查找元素 23 * @param [type] $key 24 * @return [type] 25 */ 26 public function get($key){ 27 return $this->memcache->get($key); 28 } 29 /** 30 * 增長一個條目到緩存服務器 31 * @param [type] $key 32 * @param [type] $value 33 * @param integer $expire 34 * @param integer $flag 35 * @param [type] $serializetype 36 */ 37 public function add($key,$value,$expire=0,$flag=0,$serializetype=null){ 38 if($serializetype == 'json' && is_array($value)){ 39 $value = json_encode($value); 40 } 41 $ret = $this->memcache->add($key,$value,$flag,$expire); 42 return $ret; 43 } 44 /** 45 * 清洗(刪除)已經存儲的全部的元素 46 * @return [type] 47 */ 48 public function flush(){ 49 return $this->memcache->flush(); 50 } 51 /** 52 * 從服務端刪除一個元素 53 * @param [type] delete 參數:key要刪除的元素的key 刪除該元素的執行時間 timeout若是值爲0,則該元素當即刪除。 54 * @return [type] 55 */ 56 public function delete($key){ 57 $ret = $this->memcache->delete($key,0); 58 return $ret; 59 } 60 }
而後調用dlufmemcache:緩存
1 $memcache = new dlufMemcache('127.0.0.1',11211); 2 $memcache->set('memcache','come on dluf&baidu !!!!!!'); 3 $ret = $memcache->get('memcache'); 4 echo print_r($ret,true);
運行輸出可見:服務器
http://php.net/manual/zh/class.memcache.phpide