【php开源代码栏目提醒】:网学会员为广大网友收集整理了,CacheDb.class.php,希望对大家有所帮助!
<?php
// +----------------------------------------------------------------------
// | Think
PHP // +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* 鏁版嵁搴撶被鍨嬬紦瀛樼被
CREATE TABLE THINK_CACHE (
id int(11) unsigned NOT NULL auto_increment,
cachekey varchar(255) NOT NULL,
expire int(11) NOT NULL,
data blob,
datasize int(11),
datacrc int(32),
PRIMARY KEY (id)
);
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Util
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class CacheDb extends Cache
{//绫诲畾涔夊紑濮?
/**
+----------------------------------------------------------
* 缂撳瓨鏁版嵁搴撳璞?閲囩敤鏁版嵁搴撴柟寮忔湁鏁?
+----------------------------------------------------------
* @var string
* @access protected
+----------------------------------------------------------
*/
var $db ;
/**
+----------------------------------------------------------
* 鏋舵瀯鍑芥暟
+----------------------------------------------------------
* @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;
$this->db = DB::getInstance();
$this->handler = $this->db->connect();
$this->connected = is_resource($this->handler);
$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);
$this->Q(1);
$result = $this->db->getRow('select `data`,`datacrc`,`datasize` from `'.$this->options['table'].'` where `cachekey`=\''.$name.'\' and (`expire` =-1 OR `expire`>'.time().') limit 0,1');
if(false !== $result ) {
if(is_object($result)) {
$result = get_object_vars($result);
}
if(C('DATA_CACHE_CHECK')) {//寮鍚暟鎹牎楠?
if($result['datacrc'] != md5($result['data'])) {//鏍¢獙閿欒
return false;
}
}
$content = $result['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);
$this->W(1);
if( C('DA