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

首頁 > 語言 > PHP > 正文

PHP中Memcache操作類使用方法

2024-09-04 11:44:13
字體:
來源:轉載
供稿:網友
  1. <?php  
  2.     /*  author:凹凸曼(lyc)  
  3.     /*  email: jar-c@163.com  
  4.     /*  內存緩存管理  
  5.     */ 
  6. class Yc_Memcache{  
  7.  private $memcache=null;   
  8.    
  9.  public function __construct(){  
  10.  }  
  11.  /**  
  12.     * 連接數據庫  
  13.     *  
  14.     * @param mixed $host  
  15.     * @param mixed $port  
  16.     * @param mixed $timeout  
  17.     */ 
  18.  public  function connect($host,$port=11211,$timeout=1){  
  19.   if(!function_exists(memcache_connect)){ return FALSE;}  
  20.   $this->memcache=@memcache_connect($host,$port,$timeout);  
  21.   if(emptyempty($this->memcache)){  
  22.    return FALSE;  
  23.   }else{  
  24.    return TRUE;  
  25.   }  
  26.  }  
  27.     /**  
  28.     * 存放值  
  29.     *  
  30.     * @param mixed $key  
  31.     * @param mixed $var  
  32.     * @param mixed $flag   默認為0不壓縮  壓縮狀態填寫:MEMCACHE_COMPRESSED  
  33.     * @param mixed $expire  默認緩存時間(單位秒)  
  34.     */ 
  35.  public function set($key,$var,$flag=0,$expire=10){  
  36.    
  37.   $f=@memcache_set($this->memcache,$key,$var,$flag,$expire);  
  38.   if(emptyempty($f)){  
  39.    return FALSE;  
  40.   }else{  
  41.    return TRUE;  
  42.   }  
  43.  }  
  44.     /**  
  45.     * 取出對應的key的value  
  46.     *  
  47.     * @param mixed $key  
  48.     * @param mixed $flags  
  49.     * $flags 如果此值為1表示經過序列化,  
  50.     * 但未經過壓縮,2表明壓縮而未序列化,  
  51.     * 3表明壓縮并且序列化,0表明未經過壓縮和序列化  
  52.     */ 
  53.  public function get($key,$flags=0){  
  54.   $val=@memcache_get($this->memcache,$key,$flags);  
  55.   return $val;  
  56.  }  
  57.  /**  
  58.     * 刪除緩存的key  
  59.     *  
  60.     * @param mixed $key  
  61.     * @param mixed $timeout  
  62.     */ 
  63.  public function delete($key,$timeout=1){  
  64.   $flag=@memcache_delete($this->memcache,$key,$timeout);  
  65.   return $flag;  
  66.  }  
  67.     /**  
  68.     * 刷新緩存但不釋放內存空間  
  69.     *  
  70.     */ 
  71.  public function flush(){  
  72.   memcache_flush($this->memcache);  
  73.  }  
  74.     /**  
  75.     * 關閉內存連接  
  76.     *  
  77.     */ 
  78.  public function close(){  
  79.   memcache_close($this->memcache);  
  80.  }  
  81.     /**  
  82.     * 替換對應key的value  
  83.     *  
  84.     * @param mixed $key  
  85.     * @param mixed $var  
  86.     * @param mixed $flag  
  87.     * @param mixed $expire  
  88.     */ 
  89.  public function replace($key,$var,$flag=0,$expire=1){  
  90.   $f=memcache_replace($this->memcache,$key,$var,$flag,$expire);  
  91.   return $f;  
  92.  }  
  93.     /**  
  94.     * 開啟大值自動壓縮  
  95.     *  
  96.     * @param mixed $threshold 單位b  
  97.     * @param mixed $min_saveings 默認值是0.2表示20%壓縮率  
  98.     */ 
  99.  public function setCompressThreshold($threshold,$min_saveings=0.2){  
  100.   $f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);  
  101.   return $f;  
  102.  }  
  103.     /**  
  104.     * 用于獲取一個服務器的在線/離線狀態  
  105.     *  
  106.     * @param mixed $host  
  107.     * @param mixed $port  
  108.     */ 
  109.  public function getServerStatus($host,$port=11211){  
  110.   $re=memcache_get_server_status($this->memcache,$host,$port);  
  111.   return $re;  
  112.  }  
  113.     /**  
  114.     * 緩存服務器池中所有服務器統計信息  
  115.     *  
  116.     * @param mixed $type 期望抓取的統計信息類型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}  
  117.     * @param mixed $slabid  cachedump命令會完全占用服務器通常用于 比較嚴格的調  
  118.     * @param mixed $limit 從服務端獲取的實體條數  
  119.     */ 
  120.  public function getExtendedStats($type='',$slabid=0,$limit=100){  
  121.   $re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit); //開源代碼Vevb.com 
  122.   return $re;  
  123.  }  
  124. }  
  125.    
  126. /***********測試區域********************/ 
  127. $mem=new Yc_Memcache();  
  128.    
  129. $f=$mem->connect('125.64.41.138',12000);  
  130. var_dump($f);  
  131. if($f){  
  132. // $mem->setCompressThreshold(2000,0.2);  
  133.  $mem->set('key','hello',0,30);  
  134. //        var_dump($mem->delete('key1'));  
  135.  // $mem->flush();  
  136. // var_dump($mem->replace('hao','d'));  
  137. // echo $mem->get('key');  
  138.  echo $mem->getServerStatus('127.0.0.1',12000);  
  139.  echo $mem->get('key');  
  140.  echo '<pre>';  
  141.  print_r($mem->getExtendedStats());  
  142. }  
  143.    
  144. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大新县| 阜城县| 葫芦岛市| 商南县| 平乡县| 新乡县| 栾城县| 平顶山市| 营口市| 古交市| 米易县| 龙门县| 安仁县| 宜良县| 开封县| 湟中县| 叙永县| 双流县| 二手房| 开封市| 舞阳县| 全南县| 馆陶县| 苏尼特右旗| 景宁| 化隆| 论坛| 德江县| 武隆县| 务川| 武宣县| 孟津县| 永清县| 盈江县| 五大连池市| 庄河市| 武安市| 亚东县| 麦盖提县| 和顺县| 彭州市|