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

首頁 > 開發 > PHP > 正文

PHP中Memcache操作類及用法實例

2024-05-04 23:28:53
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP中Memcache操作類及用法,以實例形式詳細分析了Memcache類連接數據庫及進行緩存操作的具體用法,非常具有實用價值,需要的朋友可以參考下
 
 

本文實例講述了PHP中Memcache操作類及用法。分享給大家供大家參考。具體分析如下:

 

復制代碼代碼如下:

<?php  
    /*  內存緩存管理  
    */ 
class Yc_Memcache{  
 private $memcache=null;   
   
 public function __construct(){  
 }  
 /**  
    * 連接數據庫  
    *  
    * @param mixed $host  
    * @param mixed $port  
    * @param mixed $timeout  
    */ 
 public  function connect($host,$port=11211,$timeout=1){  
  if(!function_exists(memcache_connect)){ return FALSE;}  
  $this->memcache=@memcache_connect($host,$port,$timeout);  
  if(emptyempty($this->memcache)){  
   return FALSE;  
  }else{  
   return TRUE;  
  }  
 }  
    /**  
    * 存放值  
    *  
    * @param mixed $key  
    * @param mixed $var  
    * @param mixed $flag   默認為0不壓縮  壓縮狀態填寫:MEMCACHE_COMPRESSED  
    * @param mixed $expire  默認緩存時間(單位秒)  
    */ 
 public function set($key,$var,$flag=0,$expire=10){  
   
  $f=@memcache_set($this->memcache,$key,$var,$flag,$expire);  
  if(emptyempty($f)){  
   return FALSE;  
  }else{  
   return TRUE;  
  }  
 }  
    /**  
    * 取出對應的key的value  
    *  
    * @param mixed $key  
    * @param mixed $flags  
    * $flags 如果此值為1表示經過序列化,  
    * 但未經過壓縮,2表明壓縮而未序列化,  
    * 3表明壓縮并且序列化,0表明未經過壓縮和序列化  
    */ 
 public function get($key,$flags=0){  
  $val=@memcache_get($this->memcache,$key,$flags);  
  return $val;  
 }  
 /**  
    * 刪除緩存的key  
    *  
    * @param mixed $key  
    * @param mixed $timeout  
    */ 
 public function delete($key,$timeout=1){  
  $flag=@memcache_delete($this->memcache,$key,$timeout);  
  return $flag;  
 }  
    /**  
    * 刷新緩存但不釋放內存空間  
    *  
    */ 
 public function flush(){  
  memcache_flush($this->memcache);  
 }  
    /**  
    * 關閉內存連接  
    *  
    */ 
 public function close(){  
  memcache_close($this->memcache);  
 }  
    /**  
    * 替換對應key的value  
    *  
    * @param mixed $key  
    * @param mixed $var  
    * @param mixed $flag  
    * @param mixed $expire  
    */ 
 public function replace($key,$var,$flag=0,$expire=1){  
  $f=memcache_replace($this->memcache,$key,$var,$flag,$expire);  
  return $f;  
 }  
    /**  
    * 開啟大值自動壓縮  
    *  
    * @param mixed $threshold 單位b  
    * @param mixed $min_saveings 默認值是0.2表示20%壓縮率  
    */ 
 public function setCompressThreshold($threshold,$min_saveings=0.2){  
  $f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);  
  return $f;  
 }  
    /**  
    * 用于獲取一個服務器的在線/離線狀態  
    *  
    * @param mixed $host  
    * @param mixed $port  
    */ 
 public function getServerStatus($host,$port=11211){  
  $re=memcache_get_server_status($this->memcache,$host,$port);  
  return $re;  
 }  
    /**  
    * 緩存服務器池中所有服務器統計信息  
    *  
    * @param mixed $type 期望抓取的統計信息類型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}  
    * @param mixed $slabid  cachedump命令會完全占用服務器通常用于 比較嚴格的調  
    * @param mixed $limit 從服務端獲取的實體條數  
    */ 
 public function getExtendedStats($type='',$slabid=0,$limit=100){  
  $re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit); 
  return $re;  
 }  
}  
   
/***********測試區域********************/ 
$mem=new Yc_Memcache();  
   
$f=$mem->connect('125.64.41.138',12000);  
var_dump($f);  
if($f){  
// $mem->setCompressThreshold(2000,0.2);  
 $mem->set('key','hello',0,30);  
//        var_dump($mem->delete('key1'));  
 // $mem->flush();  
// var_dump($mem->replace('hao','d'));  
// echo $mem->get('key');  
 echo $mem->getServerStatus('127.0.0.1',12000);  
 echo $mem->get('key');  
 echo '<pre>';  
 print_r($mem->getExtendedStats());  
}  
   
?>

 

希望本文所述對大家的PHP程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石棉县| 襄垣县| 天全县| 方山县| 静安区| 卢湾区| 晋宁县| 罗定市| 武强县| 平乐县| 南阳市| 神木县| 西城区| 张家口市| 太湖县| 闸北区| 西青区| 凤城市| 南汇区| 岳普湖县| 盱眙县| 大石桥市| 田林县| 肥东县| 乌什县| 昌黎县| 隆昌县| 迁安市| 辽源市| 随州市| 张掖市| 尚义县| 东山县| 呼伦贝尔市| 林口县| 吴忠市| 德昌县| 旌德县| 延安市| 阿鲁科尔沁旗| 隆尧县|