由于这是一个很老的答案,链接到的网站不再存在(新链接)。我会将代码复制到这里供Codeigniter旧版本使用。
自Codeigniter 2以来就有了这个答案。
在application/core中创建MY_Output.php文件,并使用下面的代码。
<?php
class MY_Output extends CI_Output
{
public function __construct()
{
parent::__construct();
}
public function _display_cache(&$CFG, &$URI)
{
$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
$uri = $CFG->item('base_url').
$CFG->item('index_page').
$URI->uri_string;
$querystrings = $_SERVER['QUERY_STRING'];
if ( $querystrings != null ) {$querystrings = "?".$querystrings;}
$uri = $uri.$querystrings;
$filepath = $cache_path.md5($uri);
if ( ! @file_exists($filepath))
{
return FALSE;
}
if ( ! $fp = @fopen($filepath, FOPEN_READ))
{
return FALSE;
}
flock($fp, LOCK_SH);
$cache = '';
if (filesize($filepath) > 0)
{
$cache = fread($fp, filesize($filepath));
}
flock($fp, LOCK_UN);
fclose($fp);
if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
{
return FALSE;
}
if (time() >= trim(str_replace('TS--->', '', $match['1'])))
{
if (is_really_writable($cache_path))
{
@unlink($filepath);
log_message('debug', "Cache file has expired. File deleted");
return FALSE;
}
}
$this->_display(str_replace($match['0'], '', $cache));
log_message('debug', "Cache file is current. Sending it to browser.");
return TRUE;
}
public function _write_cache($output)
{
$CI =& get_instance();
$path = $CI->config->item('cache_path');
$cache_path = ($path == '') ? APPPATH.'cache/' : $path;
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
{
log_message('error', "Unable to write cache file: ".$cache_path);
return;
}
$uri = $CI->config->item('base_url').
$CI->config->item('index_page').
$CI->uri->uri_string();
$querystrings = $_SERVER['QUERY_STRING'];
if ( $querystrings != null ) {$querystrings = "?".$querystrings;}
$uri = $uri.$querystrings;
$cache_path .= md5($uri);
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
{
log_message('error', "Unable to write cache file: ".$cache_path);
return;
}
$expire = time() + ($this->cache_expiration * 60);
if (flock($fp, LOCK_EX))
{
fwrite($fp, $expire.'TS--->'.$output);
flock($fp, LOCK_UN);
}
else
{
log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
return;
}
fclose($fp);
@chmod($cache_path, FILE_WRITE_MODE);
log_message('debug', "Cache file written: ".$cache_path);
}
}
清除
application/cache 中的所有缓存文件和文件夹。
原始答案(2011年)
这可以修复带查询字符串的 CodeIgniter 缓存。
CodeIgniter 缓存带查询字符串
这是泰语页面,但您可以将该代码复制并放入 application/core/MY_Output.php 文件中 :)