php专区

 首页 > php专区 > PHP应用 > php类库 > PHP中Memcache操作类使用方法 - php类库

PHP中Memcache操作类使用方法 - php类库

分享到:
【字体:
导读:
          ?php/*author:凹凸曼(lyc)/*email:jar-c@163.com/*内存缓存管理*/classYc_Memcache{private$memcache=null;publicfunction__construct(){}......

PHP中Memcache操作类使用方法

  1.     /*  author:凹凸曼(lyc)  
  2.     /*  email: jar-c@163.com  
  3.     /*  内存缓存管理  
  4.     */ 
  5. class Yc_Memcache{  
  6.  private $memcache=null;   
  7.    
  8.  public function __construct(){  
  9.  }  
  10.  /**  
  11.     * 连接数据库  
  12.     *  
  13.     * @param mixed $host  
  14.     * @param mixed $port  
  15.     * @param mixed $timeout  
  16.     */ 
  17.  public  function connect($host,$port=11211,$timeout=1){  
  18.   if(!function_exists(memcache_connect)){ return FALSE;}  
  19.   $this->memcache=@memcache_connect($host,$port,$timeout);  
  20.   if(emptyempty($this->memcache)){  
  21.    return FALSE;  
  22.   }else{  
  23.    return TRUE;  
  24.   }  
  25.  }  
  26.     /**  
  27.     * 存放值  
  28.     *  
  29.     * @param mixed $key  
  30.     * @param mixed $var  
  31.     * @param mixed $flag   默认为0不压缩  压缩状态填写:MEMCACHE_COMPRESSED  
  32.     * @param mixed $expire  默认缓存时间(单位秒)  
  33.     */ 
  34.  public function set($key,$var,$flag=0,$expire=10){  
  35.    
  36.   $f=@memcache_set($this->memcache,$key,$var,$flag,$expire);  
  37.   if(emptyempty($f)){  
  38.    return FALSE;  
  39.   }else{  
  40.    return TRUE;  
  41.   }  
  42.  }  
  43.     /**  
  44.     * 取出对应的key的value  
  45.     *  
  46.     * @param mixed $key  
  47.     * @param mixed $flags  
  48.     * $flags 如果此值为1表示经过序列化,  
  49.     * 但未经过压缩,2表明压缩而未序列化,  
  50.     * 3表明压缩并且序列化,0表明未经过压缩和序列化  
  51.     */ 
  52.  public function get($key,$flags=0){  
  53.   $val=@memcache_get($this->memcache,$key,$flags);  
  54.   return $val;  
  55.  }  
  56.  /**  
  57.     * 删除缓存的key  
  58.     *  
  59.     * @param mixed $key  
  60.     * @param mixed $timeout  
  61.     */ 
  62.  public function delete($key,$timeout=1){  
  63.   $flag=@memcache_delete($this->memcache,$key,$timeout);  
  64.   return $flag;  
  65.  }  
  66.     /**  
  67.     * 刷新缓存但不释放内存空间  
  68.     *  
  69.     */ 
  70.  public function flush(){  
  71.   memcache_flush($this->memcache);  
  72.  }  
  73.     /**  
  74.     * 关闭内存连接  
  75.     *  
  76.     */ 
  77.  public function close(){  
  78.   memcache_close($this->memcache);  
  79.  }  
  80.     /**  
  81.     * 替换对应key的value  
  82.     *  
  83.     * @param mixed $key  
  84.     * @param mixed $var  
  85.     * @param mixed $flag  
  86.     * @param mixed $expire  
  87.     */ 
  88.  public function replace($key,$var,$flag=0,$expire=1){  
  89.   $f=memcache_replace($this->memcache,$key,$var,$flag,$expire);  
  90.   return $f;  
  91.  }  
  92.     /**  
  93.     * 开启大值自动压缩  
  94.     *  
  95.     * @param mixed $threshold 单位b  
  96.     * @param mixed $min_saveings 默认值是0.2表示20%压缩率  
  97.     */ 
  98.  public function setCompressThreshold($threshold,$min_saveings=0.2){  
  99.   $f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);  
  100.   return $f;  
  101.  }  
  102.     /**  
  103.     * 用于获取一个服务器的在线/离线状态  
  104.     *  
  105.     * @param mixed $host  
  106.     * @param mixed $port  
  107.     */ 
  108.  public function getServerStatus($host,$port=11211){  
  109.   $re=memcache_get_server_status($this->memcache,$host,$port);  
  110.   return $re;  
  111.  }  
  112.     /**  
  113.     * 缓存服务器池中所有服务器统计信息  
  114.     *  
  115.     * @param mixed $type 期望抓取的统计信息类型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}  
  116.     * @param mixed $slabid  cachedump命令会完全占用服务器通常用于 比较严格的调  
  117.     * @param mixed $limit 从服务端获取的实体条数  
  118.     */ 
  119.  public function getExtendedStats($type='',$slabid=0,$limit=100){  
  120.   $re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit); //开源代码phpfensi.com 
  121.   return $re;  
  122.  }  
  123. }  
  124.    
  125. /***********测试区域********************/ 
  126. $mem=new Yc_Memcache();  
  127.    
  128. $f=$mem->connect('125.64.41.138',12000);  
  129. var_dump($f);  
  130. if($f){  
  131. // $mem->setCompressThreshold(2000,0.2);  
  132.  $mem->set('key','hello',0,30);  
  133. //        var_dump($mem->delete('key1'));  
  134.  // $mem->flush();  
  135. // var_dump($mem->replace('hao','d'));  
  136. // echo $mem->get('key');  
  137.  echo $mem->getServerStatus('127.0.0.1',12000);  
  138.  echo $mem->get('key');  
  139.  echo '
    ';  
  140.  print_r($mem->getExtendedStats());  
  141. }  
  142.    
  143. ?>
分享到:
php文件操作类,建立,写入,删除,修改,复制...
php文件操作类,建立,写入,删除,修改,复制,移动,创建目录 php文件操作类,本文件操作类可实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录.大家可参考参考. 实例1,代码如下:
php缓存数据功能的mysqli类 - php类库
php缓存数据功能的mysqli类
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……