Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 分享一个php memcache类

分享一个php memcache类

发布时间:2018-04-06   编辑:www.jquerycn.cn
本文分享一个php memcache类,有关php编程中memcache缓存的具体用法,有需要的朋友参考下。

例子,php memcache类实现代码。
 

复制代码 代码示例:

<?php
// 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();
}
}
?>

您可能感兴趣的文章:
PHP连接Memcache程序代码
PHP如何操作Memcache缓存?
yii中使用memcache的实例分享
如何在Linux服务器端的安装Memcache
PHP网站session共享几种方案
PHP调用MEMCACHE高速缓存技术实例
emlog中使用memcache缓存配置修改方法
使用memcache来保存session
PHP 连接 Memcached 服务
分享一个php memcache类

关键词: memcache  php缓存   
[关闭]