<?php
/**
* @example $mem = new Memcached();
* @example $getCache = $mem->get('test');
* @example MEMCACHE_HOST 主机
* @example MEMCACHE_PORT 端口
* @example MEMCACHE_TIMEOUT 缓存时间
*/
class Memcached {
private $memcache = null;
/**
* @desc 构造函数
*/
public function __construct()
{
$this->memcache = new Memcache;
$this->memcache->connect(MEMCACHE_HOST, MEMCACHE_PORT, MEMCACHE_TIMEOUT);
}
/**
* 兼容php4
*/
public function Memcached()
{
$this->__construct();
}
/**
* @desc 根据key获取Memcache的值
* @param string $name key名称
* @return array or string
*/
public function get($name,$isJson = true)
{
$value = $this->memcache->get($name);
if($isJson)
$value = json_decode($value, true);
return $value;
}
/**
* 设置缓存,如果存在就更新,不存在则添加,如果成功则返回 TRUE,失败则返回 FALSE。
* @param string $name key名称
* @param array or array $value value值
* @param boolean $ttl 是否压缩
* @param int $ext1 用来设置一个过期自动销毁的时间
* @return boolean
*/
public function set($name, $value, $ext1 = false, $ttl= 0)
{
return $this->memcache->set($name, $value, $ext1, $ttl);
}
/**
* 添加缓存,如果成功则返回 TRUE,失败则返回 FALSE。
* @param string $name key名称
* @param array or array $value value值
* @param boolean $ttl 是否压缩
* @param int $ext1 用来设置一个过期自动销毁的时间
* @return boolean
*/
public function add($name, $value, $ext1 = false, $ttl= 0)
{
return $this->memcache->add($name, $value , $ext1, $ttl);
}
/**
* @desc 删除缓存,如果成功则返回 TRUE,失败则返回 FALSE。
* @param string $name key名称
* @return boolean
*/
public function delete($name)
{
return $this->memcache->delete($name);
}
/**
* @desc 关闭一个Memcache对象
* @return blloean
*/
public function close()
{
return $this->memcache->close();
}
/**
* @desc Increment item's value (加法操作)
* @param string $name
* @param int $value Increment the item by value . Optional and defaults to 1.
* @return type
*/
public function increment($name , $value)
{
return $this->memcache->increment($name, $vlaue);
}
/**
* @desc decrement item's value (减法操作)
* @param string $name
* @param int $value decrement the item by value . Optional and defaults to 1.
* @return type
*/
public function decrement($name , $value)
{
return $this->memcache->decrement($name, $vlaue);
}
/**
* @desc 获取进程池中所有进程的运行系统统计
* @return array
*/
public function getExtendedStats()
{
return $this->memcache->getExtendedStats();
}
/**
* @desc 返回服务器的一些运行统计信息
* @return array
*/
public function getStats()
{
return $this->memcache->getStats();
}
/**
* @desc 清空缓存,如果成功则返回 TRUE,失败则返回 FALSE。
* @return boolean
*/
public function flush()
{
return $this->memcache->flush();
}
}
?>