|
<?php
/**
+------------------------------------------------------------------------------
* Memcache缓存类
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class CacheMemcache extends Cache
{//类定义开始
/**
* 架构函数
*/
function __construct($options='')
{
if(empty($options)) {
$options = array
(
'host' => '127.0.0.1',
'port' => 11211,
'timeout' => false,
'persistent' => false
);
}
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
$this->handler = new Memcache;
$this->connected = $options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
$this->type = strtoupper(substr(__CLASS__,6));
}
/**
* 是否连接
*/
private function isConnected()
{
return $this->connected;
}
/**
* 读取缓存
*/
public function get($name)
{
return $this->handler->get($name);
}
/**
* 写入缓存
*/
public function set($name, $value, $ttl = null)
{
if(isset($ttl) && is_int($ttl))
$expire = $ttl;
else
$expire = $this->expire;
return $this->handler->set($name, $value, 0, $expire);
}
/**
* 删除缓存
*/
public function rm($name, $ttl = false)
{
return $ttl === false ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
}
/**
* 清除缓存
*/
public function clear()
{
return $this->handler->flush();
}
}//类定义结束
其中Cache类定义:
<?php
/**
+------------------------------------------------------------------------------
* 缓存管理类
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class Cache extends Think
{//类定义开始
/**
* 是否连接
*/
protected $connected;
/**
* 操作句柄
*/
protected $handler;
/**
* 缓存存储前缀
*/
protected $prefix='~@';
/**
* 缓存连接参数
*/
protected $options = array();
/**
* 缓存类型
*/
protected $type;
/**
* 缓存过期时间
*/
protected $expire;
/**
* 连接缓存
*/
public function connect($type='memcache',$options=array())
{
$cachePath = dirname(__FILE__).'/Cache/';
$cacheClass = 'Cache'.ucwords(strtolower(trim($type)));
require_cache($cachePath.$cacheClass.'.class.php');
if(class_exists($cacheClass))
$cache = new $cacheClass($options);
else
throw_exception(L('_CACHE_TYPE_INVALID_').':'.$type);
return $cache;
}
public function __get($name) {
return $this->get($name);
}
public function __set($name,$value) {
return $this->set($name,$value);
}
public function __unset($name) {
$this->rm($name);
}
public function setOptions($name,$value) {
$this->options[$name] = $value;
}
public function getOptions($name) {
return $this->options[$name];
}
/**
* 取得缓存类实例
*/
static function getInstance()
{
$param = func_get_args();
return get_instance_of(__CLASS__,'connect',$param);
}
}//类定义结束 |
|
|