php 简单的缓存全站函数介绍
Release: 2016-06-13 10:39:29
Original
743 people have browsed it
php 简单的缓存全站函数介绍
- function cache_page($refresh=20){
- ob_start();//开启缓冲区
- $hash=sha1($_SERVER[PHP_SELF].|G|.serialize($_GET).|P|.serialize($_POST)); //缓存文件名字
- $file=dirname(__FILE__)./cache/.$hash;//缓存文件路径
- if(!file_exists($file)) {//缓存文件不存在
- register_shutdown_function(cache_page_go,$file);
- }else{// 缓存文件存在
- if( (time()-filemtime($file))>$refresh ){//缓存超时
- register_shutdown_function(cache_page_go,$file);// 调用函数
- }
- else{//正常使用缓存文件
- $f=file_get_contents($file);// 取出缓存文件内容
- echo $f.缓存的哦;//输出缓存内容
- $output=ob_get_contents(); //取出缓冲区内容
- ob_get_clean(); //清空缓冲区
- echo $output; //输出
- exit();
- }
- }
- }
-
- function cache_page_go($file){
- $output=ob_get_contents();//获取缓冲区内容
- ob_get_clean(); //清空缓冲区
- file_put_contents($file,$output,LOCK_EX);//写入缓存文件
- echo $output.新建的哦;//输出缓存内容
- exit();
- }
- ?>
-
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31