php memcache 封装类,分享一个php memcache类

例子,php memcache类实现代码。 代码示例:

// memcache

class php_memcache{

protected $handle  = null;

protected $mem  = null;

private $host   = "";

private $port   = "";

private $timeout  = 0;

private $pconnect = false;

public function __construct($host = '127.0.0.1',$port='11211',$timeout=0,$pconnect=false){

$this->host = $host;

$this->port = $port;

$this->timeout = $timeout;

$this->pconnect = $pconnect;

$this->mem     = new Memcache();

}

public function connect(){

if(!is_resource($this->handle)) {

if($this->pconnect == false){

if(empty($this->host)){

return false;

}

if(empty($this->port)){

return false;

}

$handle = $this->mem->connect($this->host,$this->port);

if(!$handle){

return false;

}else{

$this->handle = $handle;

}

}

}

return $this->handle;

}

public function set($key,$val,$flag=false,$expire=0){

if(!$this->connect()) return false;

$iFlage = 0;

if($flag == true){

$iFlage = MEMCACHE_COMPRESSED;

}

return $this->mem->set($key,$val,$iFlage,$expire);

}

public function get($key){

if(!$this->connect()) return false;

if (is_array($key)) {

$dest = array();

foreach ($key as $subkey) {

$val = $this->get($subkey);

if (!($val === false)){

$dest[$subkey] = $val;

}

}

return $dest;

} else {

return $this->mem->get($key);

}

}

public function replace($key,$val,$flag=false,$expire=0){

if(!$this->connect()) return false;

$iFlage = 0;

if($flag == true){

$iFlage = MEMCACHE_COMPRESSED;

}

return $this->mem->replace($key,$val,$iFlage,$expire);

}

public function delete($key,$time=0){

if(!$this->connect()) return false;

return $this->mem->delete($key,$time);

}

public function flush(){

if(!$this->connect()) return false;

return $this->mem->flush();

}

public function incr($key,$val=1){

if(!$this->connect()) return false;

return $this->mem->increment($key,$val);

}

public function decr($key,$val=1){

if(!$this->connect()) return false;

return $this->mem->decrement($key,$val);

}

public function getVersion(){

if(!$this->connect()) return false;

return $this->mem->getVersion();

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值