本文实例讲述了PHP中Memcache操作类及用法。分享给大家供大家参考。具体分析如下:
复制代码 代码如下:
class Yc_Memcache{
private $memcache=null;
public function __construct(){
}
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;
}
}
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;
}
}
public function get($key,$flags=0){
$val=@memcache_get($this->memcache,$key,$flags);
return $val;
}
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);
}
public function replace($key,$var,$flag=0,$expire=1){
$f=memcache_replace($this->memcache,$key,$var,$flag,$expire);
return $f;
}
public function setCompressThreshold($threshold,$min_saveings=0.2){
$f=@memcache_set_compress_threshold($this->memcache,$threshold,$min_saveings);
return $f;
}
public function getServerStatus($host,$port=11211){
$re=memcache_get_server_status($this->memcache,$host,$port);
return $re;
}
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 '
';
print_r($mem->getExtendedStats());
}
?>
希望本文所述对大家的PHP程序设计有所帮助。



