简单的方式来缓存任何php的输出结果。
if ( time () - filemtime ( ' index.cache ' ) > 2 * 60 ) // 如果缓存文件为两分钟前更新,则重新生成缓存
@ ob_start ( ' output ' ); // 使用output函数捕捉正常输出并缓存
else // 否则直接输出缓存页面
{
@ ob_start ( ' ob_gzhandler ' );
readfile ( ' index.cache ' );
exit ;
}
function output( $buffer )
{
$scriptname = $_SERVER [ ' SCRIPT_FILENAME ' ];
if ( $scriptname == '' ) $scriptname = $_SERVER [ ' PATH_TRANSLATED ' ];
@ chdir ( dirname ( $scriptname ));
if ( $fp = @ fopen ( ' index.cache ' , ' w ' ))
{
@ flock ( $fp , LOCK_EX);
@ fwrite ( $fp , $buffer );
@ fclose ( $fp );
}
if ( function_exists ( ' gzencode ' ) && ereg ( ' gzip ' , $_SERVER [ ' HTTP_ACCEPT_ENCODING ' ]))
{
header ( " Content-Encoding: gzip " );
return @ gzencode ( $buffer , 9 , FORCE_GZIP);
}
else
return $buffer ;
}