|
楼主 |
发表于 2012-4-29 17:24:40
|
显示全部楼层
本帖最后由 ciogao 于 2012-5-1 22:12 编辑
Hex 发表于 2012-4-13 12:10
你这个方法才是治本的方法。
把 HTTP HEADER 也缓存起来,这样就能缓存各种数据了。 ...
这段时间一直忙于一个项目的一系列接口的开发工作,加班甚至通宵,没有时间修改Output类,今天抽了点时间看了一下。修改完毕。分享给大家,以下是修改步骤。(HEX申请加精啊!{:soso_e100:})
首先修改一下类的注释信息:
PHP复制代码
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @author [url=mailto:ciogao@gmail.com]ciogao@gmail.com[/url] 修改于 2012-04-29 缓存多种资源类型
复制代码
对写缓存方法_write_cache的修改如下,详情见注释:
PHP复制代码
/**
* Write a Cache File
* @author ciogao@gmail.com 修改于 2012-04-29
* @access public
* @return void
*/
function _write_cache ($output)
{
$CI =& get_instance ();
$path = $CI->config->item('cache_path');
$cache_path = ($path == '') ? BASEPATH .'cache/' : $path;
if ( ! is_dir($cache_path) OR ! is_really_writable ($cache_path))
{
return;
}
$uri = $CI->config->item('base_url').
$CI->config->item('index_page').
$CI->uri->uri_string();
$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);
//=====================
//Powered by ciogao@gmail.com
//判断有无设置头,并写入 如 jsonFORMATS---> xmlFORMATS--->
if ($this->headers) {
$formats = explode('/',$this->headers[0][0]);
$formats = $formats[1].'FORMATS--->';
}
//======================
if (flock($fp, LOCK_EX ))
{
fwrite($fp, $expire.'TS--->'.$formats.$output); //写入 formats
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, DIR_WRITE_MODE );
log_message ('debug', "Cache file written: ".$cache_path);
}
复制代码
此时如果视图中使用了set_herder方法,如:
PHP复制代码
$this->output->set_header('Content-type: application/json',true);
复制代码
则缓存文件中会加入资源类型描述,生成缓存如:
PHP复制代码
1335690070TS--->jsonFORMATS--->{"code":1000,"msg":"success","data":{"17":{"custname":"\u5c01\u795e\u9879\u76ee8"," ***
复制代码
缓存成功。
接下来修改_display_cache方法,详情见注释:
PHP复制代码
/**
* Update/serve a cached file
* @author ciogao@gmail.com 修改于 2012-04-29
* @access public
* @return void
*/
function _display_cache (&$CFG, &$URI)
{
$cache_path = ($CFG->item('cache_path') == '') ? BASEPATH .'cache/' : $CFG->item('cache_path');
if ( ! is_dir($cache_path) OR ! is_really_writable ($cache_path))
{
return FALSE;
}
// Build the file path. The file name is an MD5 hash of the full URI
$uri = $CFG->item('base_url').
$CFG->item('index_page').
$URI->uri_string;
$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);
// Strip out the embedded timestamp
if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
{
return FALSE;
}
// Has the file expired? If so we'll delete it.
if (time() >= trim(str_replace('TS--->', '', $match['1'])))
{
@unlink($filepath);
log_message ('debug', "Cache file has expired. File deleted");
return FALSE;
}
//================================================================
/**
* Powered by ciogao@gmail.com 2012-07-4-29
* 获取缓存信息中的资源类型,并设置输出头信息
*/
preg_match("/(\w+FORMATS--->)/", $cache, $formats);
if ($formats) {
$formats_tem = explode('FORMATS',$formats[1]);
switch ($formats_tem[0]){
case 'json':
$header = 'application/json'; //jsonRPC
break;
case 'xml':
$header = 'application/xml'; //xmlRPC
break;
case 'plain':
$header = 'text/plain'; //PHP数组
break;
default:
$header = 'test/html'; //其他判断为html
}
$cache = str_replace($formats[1],'',$cache);
$this->set_header('Content-type: '.$header,true);
}
//================================================================
// Display the cache
$this->_display (str_replace($match['0'], '', $cache));
log_message ('debug', "Cache file is current. Sending it to browser.");
return TRUE;
}
复制代码
OK,大功告成。
使用不明白的地方可以联系我,以下是联系方式:
ciogao@gmail.com msn或email均可
新浪微博 @阳光蝙蝠
|
评分
-
查看全部评分
|