php頁面緩存的使用方法

<?php
/**ob_start();
ob_get_contents();
ob_end_flush();
**
**php頁面緩存的使用方法,php頁面緩存的小例子,如果數據庫查詢量較大,可以用cache來解決
**/
	function cache_start($dir,$expiretime){
		$filename = $dir.'\\'.sha1($_SERVER['REQUEST_URI']).'.html';
		ob_start();
		
		if(file_exists($filename) && (time()- filemtime($filename)<$expiretime)){
			include($filename);
			ob_end_flush();
			exit();
		}
		mkdir($dir);
	}

	function cache_end($dir){
		$filename = $dir.'\\'.sha1($_SERVER['REQUEST_URI']).'.html';
		echo $filename;
		
		$fp = fopen($filename,'w');
		fwrite($fp,ob_get_contents());
		fclose($fp);
		
		ob_end_flush();
	
	}
$dir = "D:\\\\php";
cache_start($dir, 10);
cache_end($dir);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章