国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP調(diào)用MEMCACHE高速緩存技術(shù)實(shí)例

2024-05-04 21:51:00
字體:
供稿:網(wǎng)友

在項(xiàng)目中,涉及大訪問量時(shí),合理的使用緩存能減輕數(shù)據(jù)庫的壓力,同時(shí)提升用戶體驗(yàn)。即在非實(shí)時(shí)性的需求的前提下,一小段時(shí)間內(nèi)(若干秒),用于顯示的數(shù)據(jù)從緩存中獲取的,而不用直接讀取數(shù)據(jù)庫,能有效的減少數(shù)據(jù)庫的讀取壓力。這里記錄一下php語言使用memcache的情形:

首先,我們建立一個(gè)memcachepool,可以根據(jù)不同的配置讀取,生成不同的memcache實(shí)例。用到$memcache->addServer($host,$port,$flag);向連接池中添加一個(gè)memcache服務(wù)器。代碼示例如下:

  1. class memcachePool{ 
  2.  
  3.      private static $instance
  4.  
  5.      private $memcacheList = array(); 
  6.  
  7.     private function __construct(){ 
  8.  
  9.   
  10.  
  11.     } 
  12.  
  13.      public static function getInstance(){ 
  14.  
  15.          if(self::$instance != null) 
  16.  
  17.              return self::$instance
  18.  
  19.          self::$instance = new memcachePool(); 
  20.  
  21.          return self::$instance
  22.  
  23.      } 
  24.  
  25.     /** 
  26.  
  27.      * get memcache object from pool 
  28.  
  29.      * @param  [type] $host 服務(wù)器 
  30.  
  31.      * @param  [type] $port 端口 
  32.  
  33.      * @param  [type] $flag 控制是否使用持久化連接。默認(rèn)TRUE 
  34.  
  35.      * @return [type] 
  36.  
  37.      */ 
  38.  
  39.      public function getMemcache($host,$port,$flag){ 
  40.  
  41.          if(isset($this->memcacheList[$host.$port])) 
  42.  
  43.              return $this->memcacheList[$host.$port]; 
  44.  
  45.   
  46.  
  47.         $memcache = new Memcache(); 
  48.  
  49.         // 向連接池中添加一個(gè)memcache服務(wù)器 
  50.  
  51.         $memcache->addServer($host,$port,$flag); 
  52.  
  53.         //開啟大值自動(dòng)壓縮,第一個(gè)參數(shù)表示處理數(shù)據(jù)大小的臨界點(diǎn),第二個(gè)參數(shù)表示壓縮的比例,默認(rèn)為0.2 
  54.  
  55.         $memcache->setCompressThreshold(2000,0.2); 
  56.  
  57.         $this->memcacheList[$host.$port] = $memcache
  58.  
  59.         return $memcache
  60.  
  61.      } 
  62.  
  63.  } 

接著實(shí)現(xiàn)一個(gè)包含memcache常用方法如add,set,get,flush,delete等的方法類,這里命名為dlufmemcache

  1. class dlufMemcache{ 
  2.  
  3.      private $memcache = null; 
  4.  
  5.      function __construct($host,$port){ 
  6.  
  7.   
  8.  
  9.        $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true); 
  10.  
  11.      } 
  12.  
  13.     /** 
  14.  
  15.      * memcache set value 
  16.  
  17.      * @param [type]  $key 鍵 
  18.  
  19.      * @param [type]  $value 值 
  20.  
  21.      * @param integer $expire  到期的時(shí)間,如果此值設(shè)置為0表明此數(shù)據(jù)永不過期 
  22.  
  23.      * @param integer $flag 標(biāo)志位 使用MEMCACHE_COMPRESSED指定對值進(jìn)行壓縮(使用zlib) 
  24.  
  25.      * @param [type]  $serializetype 
  26.  
  27.      */ 
  28.  
  29.      public function set($key,$value,$expire=0,$flag=0,$serializetype=null){ 
  30.  
  31.         if($serializetype == 'json' && is_array($value)){ 
  32.  
  33.             $value = json_encode($value); 
  34.  
  35.         } 
  36.  
  37.          $this->memcache->set($key,$value,$flag,$expire); 
  38.  
  39.      } 
  40.  
  41.     /** 
  42.  
  43.      * 從服務(wù)端查找元素 
  44.  
  45.      * @param  [type] $key 
  46.  
  47.      * @return [type] 
  48.  
  49.      */ 
  50.  
  51.      public function get($key){ 
  52.  
  53.          return $this->memcache->get($key); 
  54.  
  55.      } 
  56.  
  57.     /** 
  58.  
  59.      * 增加一個(gè)條目到緩存服務(wù)器 
  60.  
  61.      * @param [type]  $key 
  62.  
  63.      * @param [type]  $value 
  64.  
  65.      * @param integer $expire 
  66.  
  67.      * @param integer $flag 
  68.  
  69.      * @param [type]  $serializetype 
  70.  
  71.      */ 
  72.  
  73.     public function add($key,$value,$expire=0,$flag=0,$serializetype=null){ 
  74.  
  75.         if($serializetype == 'json' && is_array($value)){ 
  76.  
  77.             $value = json_encode($value); 
  78.  
  79.         } 
  80.  
  81.         $ret = $this->memcache->add($key,$value,$flag,$expire); 
  82.  
  83.         return $ret
  84.  
  85.     } 
  86.  
  87.     /** 
  88.  
  89.      * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素 
  90.  
  91.      * @return [type] 
  92.  
  93.      */ 
  94.  
  95.     public function flush(){ 
  96.  
  97.         return $this->memcache->flush(); 
  98.  
  99.     } 
  100.  
  101.     /** 
  102.  
  103.      *  從服務(wù)端刪除一個(gè)元素 
  104.  
  105.      * @param  [type] delete 參數(shù):key要?jiǎng)h除的元素的key 刪除該元素的執(zhí)行時(shí)間 timeout如果值為0,則該元素立即刪除。 
  106.  
  107.      * @return [type] 
  108.  
  109.      */ 
  110.  
  111.     public function delete($key){ 
  112.  
  113.         $ret = $this->memcache->delete($key,0); 
  114.  
  115.         return $ret
  116.  
  117.     } 
  118.  
  119.  } 

然后調(diào)用dlufmemcache:

  1. $memcache = new dlufMemcache('127.0.0.1',11211); 
  2.  
  3. $memcache->set('memcache','come on dluf&baidu !!!!!!'); 
  4.  
  5. $ret = $memcache->get('memcache'); 
  6.  
  7. echo print_r($ret,true);

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 依安县| 万全县| 中宁县| 武平县| 深圳市| 府谷县| 塘沽区| 五台县| 磐石市| 凌海市| 堆龙德庆县| 探索| 鄢陵县| 濉溪县| 伊吾县| 浏阳市| 房产| 宝山区| 乐陵市| 盐源县| 阿坝| 东乌珠穆沁旗| 台南县| 遵化市| 涿鹿县| 东辽县| 永昌县| 文昌市| 五河县| 安塞县| 南川市| 沾化县| 边坝县| 隆安县| 甘南县| 三门峡市| 当雄县| 湘潭县| 阜阳市| 油尖旺区| 抚顺市|