优化后的缓存文件。
适用于任何php网站。
从根本解决你网站速度问题!
<?php
/******************
require(ECMS_PATH . 'cache/content.php');
*******************************************/
//缓存存放目录
define('CACHE_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacheFile');
//缓存文件后缀
define('CACHE_FIX','.php');
date_default_timezone_set("Asia/Shanghai");
// 获取当前URL路径信息
$query_string = $_SERVER['QUERY_STRING'];
// 解析路径,获取顶级栏目
$url_path = '';
if (!empty($query_string)) {
// 解析查询字符串,去除参数部分
$query_parts = explode('&', $query_string);
// 假设第一个参数是路径信息(根据ThinkPHP的路由规则)
$first_param = $query_parts[0];
if (strpos($first_param, '/') !== false) {
$url_path = $first_param;
} else {
// 如果不是路径形式,可能是参数,则根据实际情况处理
$url_path = $query_string;
}
}
// 提取顶级栏目
$top_category = 'index'; // 默认为首页
if (!empty($url_path)) {
// 移除可能的文件扩展名
$url_path = preg_replace('/\.(html|htm|php)$/i', '', $url_path);
// 按斜杠分割路径
$path_parts = explode('/', $url_path);
// 获取第一个非空的部分作为顶级栏目
foreach ($path_parts as $part) {
if (!empty($part)) {
$top_category = $part;
break;
}
}
// 如果顶级栏目包含特殊字符或过长,使用md5简化
if (strlen($top_category) > 50 || preg_match('/[^\w\-\.]/', $top_category)) {
$top_category = md5($top_category);
}
}
// 生成缓存文件名和路径
$cache_key = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$CacheName = $cache_key . CACHE_FIX;
// 计算子目录(md5倒数第一个字符)
$sub_dir = substr($cache_key, -1, 1);
// 构建完整的缓存目录和路径
$CacheDir = CACHE_ROOT . DIRECTORY_SEPARATOR . $top_category . DIRECTORY_SEPARATOR . $sub_dir;
$CacheUrl = $CacheDir . DIRECTORY_SEPARATOR . $CacheName;
// 检查缓存是否存在
if(file_exists($CacheUrl)){
echo str_replace("{time181}",date("Y-m-d h:i:m",time()-1440),gzuncompress(file_get_contents($CacheUrl)));
exit;
}
// 创建缓存目录(如果需要)
if(!file_exists($CacheDir)){
// 创建缓存根目录
if(!file_exists(CACHE_ROOT)){
mkdir(CACHE_ROOT,0777, true);
chmod(CACHE_ROOT,0777);
}
// 创建顶级栏目目录
$top_category_dir = CACHE_ROOT . DIRECTORY_SEPARATOR . $top_category;
if(!file_exists($top_category_dir)) {
mkdir($top_category_dir,0777, true);
chmod($top_category_dir,0777);
}
// 创建子目录
mkdir($CacheDir,0777, true);
chmod($CacheDir,0777);
}
// 缓存输出函数
function AutoCache($contents){
global $CacheUrl;
$fp = fopen($CacheUrl,'wb');
$contents = "<!--缓存插件作者QQ:181021679 ".(date("Y-m-d H:i:s", time()))."-->\r\n" . $contents;
fwrite($fp, gzcompress($contents));
fclose($fp);
chmod($CacheUrl,0777);
return str_replace("{time181}", date("Y-m-d h:i:m",time()-1440), $contents);
}
ob_start('AutoCache');
clearstatcache();
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END






![表情[aini]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/aini.gif)
![表情[ciya]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/ciya.gif)
![表情[xia]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/xia.gif)


暂无评论内容