- 普通 - 未处理
ThinkPHP2.1SVN版本的问题挺多的(不是在埋怨),用SVN版本感受新功能,同时帮官方测试一下BUG。这次发现的BUG是Cli下使用缓存类的问题。
$Cache = Cache::getInstance('Db');
报无法找到Db类(驱动)。
闲着没事,自己改了一下CacheDb.class.php。
/**
+------------------------------------------------------------------------------
* 数据库类型缓存类
CREATE TABLE IF NOT EXISTS `cld_caches` (
`cachekey` varchar(255) NOT NULL,
`expire` int(11) NOT NULL,
`data` blob,
`datasize` int(11) DEFAULT NULL,
`datacrc` int(32) DEFAULT NULL,
PRIMARY KEY (`cachekey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st
* @version $Id$
+------------------------------------------------------------------------------
*/
class CacheDb extends Cache
{//类定义开始
/**
+----------------------------------------------------------
* 缓存数据库对象 采用数据库方式有效
+----------------------------------------------------------
* @var string
* @access protected
+----------------------------------------------------------
*/
var $connection;
/**
+----------------------------------------------------------
* 架构函数
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
function __construct($options='')
{
if(empty($options)){
$options= array
(
'db' => C('DB_NAME'),
'table' => C('DATA_CACHE_TABLE'),
'expire' => C('DATA_CACHE_TIME'),
);
}
$this->options = $options;
$connection = mysql_connect(C('DB_HOST'), C('DB_USER'), C('DB_PWD'));
mysql_select_db($this->options['db']);
$this->connection = $connection;
$this->connected = is_resource($this->connection);
$this->type = strtoupper(substr(__CLASS__,6));
}
/**
+----------------------------------------------------------
* 是否连接
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
private function isConnected()
{
return $this->connected;
}
/**
+----------------------------------------------------------
* 读取缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缓存变量名
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function get($name)
{
// 参数
$name = addslashes($name);
$now = time();
// SQL
$sql = "SELECT `data`,`datacrc`,`datasize`,`expire` FROM `{$this->options['table']}` WHERE `cachekey`='{$name}' LIMIT 0,1";
$rs = mysql_query($sql, $this->connection);
if(false !== $rs) {
$row = mysql_fetch_assoc($rs);
// 检测是否过期
if($row['expire']!='-1' && $row['expire'] < $now){
// 删除数据库记录
$this->rm($name);
return false;
}
if(C('DATA_CACHE_CHECK')) {//开启数据校验
if($row['datacrc'] != md5($row['data'])) {//校验错误
return false;
}
}
$content = $row['data'];
if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
}
$content = unserialize($content);
return $content;
}
else {
return false;
}
}
/**
+----------------------------------------------------------
* 写入缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function set($name, $value, $expireTime=0)
{
$data = serialize($value);
$name = addslashes($name);
if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data,3);
}
if(C('DATA_CACHE_CHECK')) {//开启数据校验
$crc = md5($data);
}else {
$crc = '';
}
$expire = !empty($expireTime)? $expireTime : $this->options['expire'];
$map = array();
$map['cachekey'] = $name;
$map['data'] = $data ;
$map['datacrc'] = $crc;
$map['expire'] = ($expire==-1)?-1: (time()+$expire) ;//缓存有效期为-1表示永久缓存
$map['datasize'] = strlen($data);
foreach($map as $field=>$value){
$values[] = "`{$field}`='".addslashes($value)."'";
}
// 更新记录
$sql = "REPLACE INTO `{$this->options['table']}` SET " . join(',', $values);
$rs = mysql_query($sql, $this->connection);
if($rs) {
return true;
}else {
return false;
}
}
/**
+----------------------------------------------------------
* 删除缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $name 缓存变量名
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function rm($name)
{
$name = addslashes($name);
$sql = "DELETE FROM `{$this->options['table']}` WHERE `cachekey`='{$name}'";
return mysql_query($sql, $this->connection);
}
/**
+----------------------------------------------------------
* 清除缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function clear()
{
$sql = "TRUNCATE TABLE `{$this->options['table']}`";
return mysql_query($sql, $this->connection);
}
}//类定义结束
?> 