Mem类代码:
class Mem
{
//类型是memcache或memcached
private $type = 'Memcached';
//会话
private $m;
//缓存时间,0代表永久
private $time = 0;
//错误信息
private $error;
//调试模式
private $debug = 'true';
public function __construct()
{
if (!class_exists($this->type))
{
$this->error = 'No '.$this->type;
return false;
}
else
{
$this->m = new $this->type;
}
}
//添加服务器
//$arr应该为数组
public function addServer($arr)
{
$this->m->addServers($arr);
}
//数据操作
public function s($key, $value = '', $time = 0)
{
$number = func_num_args();
if($number == 1)
{
//get
return $this->get($key);
}
else if ($number >= 2)
{
if ($value === NULL)
{
//delete
$this->delete($key);
}
else
{
//set
$this->set($key, $value, $time);
}
}
}
//set
private function set($key, $value, $time = 0)
{
if ($time === NULL)
{
$time = $this->time;
}
$this->m->set($key, $value, $time);
if ($this->debug)
{
if ($this->m->getResultCode() != 0)
{
return false;
}
}
}
//get
private function get($key)
{
$result = $this->m->get($key);
if ($this->debug)
{
if ($this->m->getResultCode() != 0)
{
return false;
}
else
{
return $result;
}
}
else
{
return $result;
}
}
//delete
private function delete($key)
{
$this->m->delete($key);
}
//获取错误信息
public function getError()
{
if($this->error)
{
return $this->error;
}
else
{
return $this->m->getResultMessage();
}
}
}
Mem类测试代码: