<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Database Cache Class
* 数据库缓存类
*
* @category Database
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/database/
*/
class CI_DB_Cache {
var $CI; //超级CI类
var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
//允许通过db对象,使多个数据库连接并返回DB对象可以支持
/**
* Constructor
*
* Grabs the CI super object instance so we can access it.
* 抓住CI超级对象实例,这样我们就可以访问它。
*/
function __construct(&$db)
{
// Assign the main CI object to 分配主要CI对象 $this->CI
// and load the file helper since we use it a lot 并加载该文件,因为我们使用了很多帮手
$this->CI =& get_instance();
$this->db =& $db;
$this->CI->load->helper('file'); //加载的是文件帮手类
}
// --------------------------------------------------------------------
/**
* Set Cache Directory Path
* 设置缓存目录路径
* @access public
* @param string the path to the cache directory 在高速缓存目录的路径...
* @return bool
*/
function check_path($path = '')
{
//如果路径为空
if ($path == '')
{
//如果当前的db->cachedir也为空值
if ($this->db->cachedir == '')
{
return $this->db->cache_off(); //我干,cache_off()放在什么地方了,咱找不到,应该是返回cache的状态
//关闭缓存
}
//设置db类中的cachedir为$path路径
$path = $this->db->cachedir;
}
// Add a trailing slash to the path if needed 如果需要添加尾随斜线路径
//这句不懂,不做备注
$path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
//目录是否存在并且是否有定入权限
if ( ! is_dir($path) OR ! is_really_writable($path))
{
// If the path is wrong we'll turn off caching
// 如果路径是错误的,我们将关闭缓存
return $this->db->cache_off();
}
//设置路径到db->cachedir中
$this->db->cachedir = $path;
return TRUE;
}
// --------------------------------------------------------------------
/**
* Retrieve a cached query
* 检索缓存查询
*
* The URI being requested will become the name of the cache sub-folder.
* An MD5 hash of the SQL statement will become the cache file name
* 所请求的URI,将成为高速缓存中的子文件夹的名称。
* 的MD5哈希值的SQL语句,将成为高速缓存的文件名
*
* @access public
* @return string
*/
function read($sql)
{
//如果找不到缓存目录,直接关闭缓存
if ( ! $this->check_path())
{
return $this->db->cache_off();
}
//uri->segment(1) 取得url的第一个参数,也就是处理器的名称
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
//取得url的第二个参数,也就是处理函数的名称
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
//然后用cachedir.$segment_one.'+'.$segment_two./md5($sql)名称
$filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
//如果没有读取到缓存内容,直接返回空
if (FALSE === ($cachedata = read_file($filepath)))
{
return FALSE;
}
//然后返序列化返回
//unserialize() unserialize()
return unserialize($cachedata);
}
// --------------------------------------------------------------------
/**
* Write a query to a cache file
* 写一个查询到缓存文件
* @access public
* @return bool
*/
function write($sql, $object)
{
if ( ! $this->check_path())
{
return $this->db->cache_off();
}
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
$dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
$filename = md5($sql); //文件名
//如果目录不存在
if ( ! @is_dir($dir_path))
{
//如果创建目录失败,直接返回
if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
{
return FALSE;
}
//设置该目录的可写权限
@chmod($dir_path, DIR_WRITE_MODE);
}
//将查询结果经过 serialize()序列化放入到缓存文件中
//file_helper.php function
if (write_file($dir_path.$filename, serialize($object)) === FALSE)
{
return FALSE;
}
//然后设置该缓存文件的访问权限
@chmod($dir_path.$filename, FILE_WRITE_MODE);
return TRUE;
}
// --------------------------------------------------------------------
/**
* Delete cache files within a particular directory
* 在一个特定的目录中删除缓存文件
* @access public
* @return bool
*/
function delete($segment_one = '', $segment_two = '')
{
if ($segment_one == '')
{
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
}
if ($segment_two == '')
{
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
}
$dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
delete_files($dir_path, TRUE); //file_helper.php function
}
// --------------------------------------------------------------------
/**
* Delete all existing cache files
* 删除所有现有的缓存文件
* @access public
* @return bool
*/
function delete_all()
{
delete_files($this->db->cachedir, TRUE); //file_helper.php function
}
}
/* End of file DB_cache.php */
/* Location: ./system/database/DB_cache.php */