文件缓存
2016-03-16 10:39
429 查看
不多说,看代码
Head.php
foot.php
test.php
//其他看到的很全的php文件缓存
看http://www.cnblogs.com/coolid/archive/2012/09/14/2684373.html 链接
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~"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title><?php echo $title?></title> </head> <body> <?php echo "是大方大方"; ?> </body> </html> <?php include 'foot.php'; ?>
//其他看到的很全的php文件缓存
看http://www.cnblogs.com/coolid/archive/2012/09/14/2684373.html 链接
相关文章推荐
- Angular自定义指令(进阶)
- 阅读《Android 从入门到精通》(31)——Intent
- java客户端Ip获取
- ccnu_2015级训练赛(1)题目解答
- java基础01
- java实例化代码------------------监听事件
- MemCache超详细解读
- 滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(3月16日)
- 编写一个能自动生成小学四则运算题目的程序
- mysql2redis
- 0316复利计算
- poj 2135 最小费用最大流模板题
- 定位unity在rk3288平台上音频解码无效的bug---定位终成功
- 使用SQLite数据库
- [linux]查看机器有几个cpu,是否支持64位
- PHP手册-use关键字
- IA-32寄存器
- HDU As Easy As A+B
- iOS编程Cookbook第19章最后一个例子不能正常工作的解决办法
- iOS编程Cookbook第19章最后一个例子不能正常工作的解决办法