Home  >  Article  >  Backend Development  >  文件缓存,php文件缓存_PHP教程

文件缓存,php文件缓存_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:57:191117browse

文件缓存,php文件缓存

不多说,看代码

Head.php

php
define('CACHE_EXPIRE',1800);  //设置过期时间
define('CACHE_PATH',$_SERVER['DOCUMENT_ROOT'].'/cache/'); //设置缓存目录
define('REFRESH_EXPIRE',10);  
is_dir(CACHE_PATH) or mkdir(CACHE_PATH,0777);

//该方法是获得用户的刷新次数,如果用户连续两次刷新,那么就显示最新内容 function refreshTimes(){ $currentUrl
='http://'.$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI']; if(isset($_COOKIE['refreshTimes'])){ $cookArray=explode("\t",$_COOKIE['refreshTimes']); if(isset($cookArray[1]) and $_SERVER['REQUEST_TIME'] - (int)$cookArray[1] < REFRESH_EXPIRE and isset($cookArray[0]) and $cookArray[0] == $currentUrl){ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t".(string)((int)$cookArray[2]+1),0,'/'); return (int)$cookArray[2]; }else{ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t1",0,'/'); return 0; } }else{ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t1",0,'/'); return 0; } } $key = md5($_SERVER['REQUEST_URI']); $path = CACHE_PATH.$key; if(is_file($path) and filemtime($path)+CACHE_EXPIRE > $_SERVER['REQUEST_TIME'] and refreshTimes() == 0 ){ readfile($path); exit(); } ob_start(); ?>

foot.php

php

$html = ob_get_contents();
ob_clean();

file_put_contents($path,$html);

echo $html;

?>

test.php

php
include 'head.php';

$title = "Hello world~";
?>
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
"http://www.w3.org/1999/xhtml">

"Content-Type" content="text/html; charset=gb2312" />
<?php echo $title?>

php echo "是大方大方"; ?> php include 'foot.php'; ?>

 

//其他看到的很全的php文件缓存

看http://www.cnblogs.com/coolid/archive/2012/09/14/2684373.html 链接

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1109846.htmlTechArticle文件缓存,php文件缓存 不多说,看代码 Head.php ? phpdefine( ' CACHE_EXPIRE ' , 1800 ); //设置过期时间define( ' CACHE_PATH ' ,$_SERVER[ ' DOCUMENT_ROOT ' ]. ' /...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn