Maison  >  Article  >  développement back-end  >  PHP缓存页面方法

PHP缓存页面方法

WBOY
WBOYoriginal
2016-06-20 13:03:421205parcourir

PHP缓存页面函数。

<p>/*****************************************************************</p>-- 函数名:cache_page(包括cache_page_go)<br />-- 作  用:轻松快速缓存全站<br />-- 参  数:缓存时间(单位:秒)<br />-- 返回值:输出内容<br />-- 实  例:cache_page(300);  函数使用在页面的最上方<br />*******************************************************************/<br /><br />function cache_page($refresh=20){<br />	ob_start();//开启缓冲区<br />	$temp=sha1($_SERVER['PHP_SELF'].'|G|'.serialize($_GET).'|P|'.serialize($_POST));//缓存文件名字<br />	$temp=dirname(__FILE__).'/cache/'.$temp;//缓存文件路径<br />	if(!file_exists($temp)){//缓存文件不存在<br />		register_shutdown_function('cache_page_go',$temp);<br />	}else{//缓存文件存在<br />		if((time()-filemtime($temp))>$refresh ){//缓存超时<br />			register_shutdown_function('cache_page_go',$temp);//调用函数<br />		}else{//正常使用缓存文件<br />			$temp=file_get_contents($temp);//取出缓存文件内容<br />			echo $temp;//输出缓存内容<br />			$temp=ob_get_contents();//取出缓冲区内容<br />			ob_get_clean();    //清空缓冲区<br />			echo $temp;      //输出<br />			unset($temp,$refresh);/*注销变量*/<br />			exit();<br />		}<br />	}<br /><p>}</p><p><br /></p>function cache_page_go($file){<br />	$output=ob_get_contents();//获取缓冲区内容<br />	ob_get_clean();           //清空缓冲区<br />	file_put_contents($file,$output,LOCK_EX);//写入缓存文件<br />	echo $output;//输出缓存内容<br />	unset($output,$file);/*注销变量*/<br />	exit();<br /><p>}</p>

建议将该函数放置在页面的最开始处


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn