Home >Backend Development >PHP Tutorial >Memcache operation classes and usage examples in PHP, phpmemcache_PHP tutorial

Memcache operation classes and usage examples in PHP, phpmemcache_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:51796browse

Memcache operation classes and usage examples in PHP, phpmemcache

The examples in this article describe the Memcache operation classes and usage in PHP. Share it with everyone for your reference. The specific analysis is as follows:

Copy code The code is as follows:

    /*  内存缓存管理 
    */
class Yc_Memcache{ 
 private $memcache=null;  
  
 public function __construct(){ 
 } 
 /**
* Connect to database
* *
* @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; 
  } 
 } 
    /**
*Storage value
* *
* @param mixed $key
* @param mixed $var
* @param mixed $flag The default is 0, no compression. Fill in the compression status: MEMCACHE_COMPRESSED
* @param mixed $expire Default cache time (in seconds)
​*/
 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; 
  } 
 } 
    /**
* Get the value of the corresponding key
* *
* @param mixed $key
* @param mixed $flags
* $flags If this value is 1, it means serialization,
* but not compressed, 2 indicates compression without serialization,
* 3 indicates compression and serialization, 0 indicates no compression or serialization
​*/
 public function get($key,$flags=0){ 
  $val=@memcache_get($this->memcache,$key,$flags); 
  return $val; 
 } 
 /**
* Delete cached key
* *
* @param mixed $key
* @param mixed $timeout
​*/
 public function delete($key,$timeout=1){ 
  $flag=@memcache_delete($this->memcache,$key,$timeout); 
  return $flag; 
 } 
    /**
* Refresh cache without releasing memory space
* *
​*/
 public function flush(){ 
  memcache_flush($this->memcache); 
 } 
    /**
* Close memory connection
* *
​*/
 public function close(){ 
  memcache_close($this->memcache); 
 } 
    /**
* Replace the value of the corresponding key
* *
* @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; 
 } 
    /**
* Turn on automatic compression for large values ​​
* *
* @param mixed $threshold unit b
* @param mixed $min_saveings The default value is 0.2, which means 20% compression rate
​*/
 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; 
 } 
    /**
* Cache statistics of all servers in the server pool
* *
* @param mixed $type The type of statistical information expected to be captured. The values ​​that can be used are {reset, malloc, maps, cachedump, slabs, items, sizes}
* @param mixed $slabid The cachedump command will completely occupy the server and is usually used for more stringent tuning
* @param mixed $limit The number of entities obtained from the server
​*/
 public function getExtendedStats($type='',$slabid=0,$limit=100){ 
  $re=memcache_get_extended_stats($this->memcache,$type,$slabid,$limit);
  return $re; 
 } 

  
/***********TEST AREA*************************/
$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 '
';  
 print_r($mem->getExtendedStats()); 

  
?>

希望本文所述对大家的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/926868.htmlTechArticlePHP中Memcache操作类及用法实例,phpmemcache 本文实例讲述了PHP中Memcache操作类及用法。分享给大家供大家参考。具体分析如下: 复制代码 代码...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn