優
化後的緩存文件。
適用於任何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();更新後的版本。
<?php
/******************
require(ECMS_PATH . '/cache.php');
*******************************************/
if (1 == 1) {
// 緩存存放目錄
define('CACHE_ROOT', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'cacheFile');
// 緩存文件後綴
define('CACHE_FIX', '.php');
date_default_timezone_set("Asia/Shanghai");
// 創建緩存根目錄(如果不存在)
if (!file_exists(CACHE_ROOT)) {
if (!mkdir(CACHE_ROOT, 0755, true)) {
error_log('無法創建緩存目錄: ' . CACHE_ROOT);
} else {
chmod(CACHE_ROOT, 0755);
}
}
/**
* 頁面類型識別(用於分目錄存儲)
*/
function getPageType() {
$requestUri = $_SERVER['REQUEST_URI'];
$scriptName = $_SERVER['SCRIPT_NAME'];
if (strpos($scriptName, 'index.php') !== false || $requestUri === '/' || $requestUri === '/index.php') {
return 'index';
}
if (strpos($requestUri, '/singer/') !== false) {
return 'singer';
}
if (strpos($requestUri, '/jieshuo/') !== false) {
return 'jieshuo';
}
if (strpos($scriptName, 'content.php') !== false || strpos($requestUri, '/lyrics/') !== false) {
return 'content';
}
if (strpos($scriptName, 'list.php') !== false || strpos($requestUri, '/list/') !== false) {
return 'list';
}
if (strpos($scriptName, 'search.php') !== false) {
return 'search';
}
return 'other';
}
/**
* 獲取緩存文件路徑
*/
function getCacheFileName() {
$pageType = getPageType();
$cacheKey = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) . CACHE_FIX;
$cacheDir = CACHE_ROOT . DIRECTORY_SEPARATOR . $pageType . DIRECTORY_SEPARATOR . substr($cacheKey, 0, 1);
$cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $cacheKey;
return [
'dir' => $cacheDir,
'file' => $cacheFile,
'type' => $pageType
];
}
/**
* 讀取緩存內容(直接解壓,無元數據)
*/
function readCache($cacheFile) {
if (!file_exists($cacheFile)) {
return false;
}
$compressed = file_get_contents($cacheFile);
if ($compressed === false) return false;
$decompressed = @gzuncompress($compressed);
if ($decompressed === false) return false;
return $decompressed;
}
/**
* 寫入緩存內容
* 在內容最前面添加緩存生成時間的HTML註釋
*/
function writeCache($cacheFile, $content) {
$dir = dirname($cacheFile);
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
// 添加頭部註釋,記錄緩存生成時間
$header = '<!-- 緩存生成時間:' . date('Y-m-d H:i:s') . ' -->' . "\n";
$compressed = gzcompress($header . $content);
return file_put_contents($cacheFile, $compressed) !== false;
}
/**
* 生成僞發佈時間(近三天內,同頁面同一天不變)
*/
function getTime181Value() {
$today = date('Y-m-d');
$url = $_SERVER['REQUEST_URI'];
$seed = $url . $today;
$hash = md5($seed);
$hex = substr($hash, 0, 8);
$offset = hexdec($hex) % 172800; // 0~2天的秒數
$base = strtotime($today . ' 00:00:00');
$fakeTime = $base - $offset;
return date('Y-m-d H:i:s', $fakeTime);
}
$cacheInfo = getCacheFileName();
$CacheDir = $cacheInfo['dir'];
$CacheUrl = $cacheInfo['file'];
$pageType = $cacheInfo['type'];
// 緩存命中:直接讀取並輸出,不做任何元數據更新
if (http_response_code() === 200 && file_exists($CacheUrl)) {
$content = readCache($CacheUrl);
if ($content !== false) {
// 替換佔位符後輸出(注意:緩存中已包含頭部註釋)
echo str_replace("{time181}", getTime181Value(), $content);
exit;
}
}
// 創建緩存子目錄(若不存在)
if (!file_exists($CacheDir)) {
if (!mkdir($CacheDir, 0755, true)) {
error_log('無法創建緩存子目錄: ' . $CacheDir);
} else {
chmod($CacheDir, 0755);
}
}
/**
* 自動緩存回調:替換佔位符並寫入緩存(寫入時會自動添加頭部註釋)
*/
function AutoCache($contents) {
global $CacheUrl;
// 替換佔位符爲僞發佈時間
$replaced = str_replace("{time181}", getTime181Value(), $contents);
// 僅當狀態碼爲200且內容非空時緩存
if (http_response_code() === 200 && !empty($replaced)) {
writeCache($CacheUrl, $replaced);
}
return $replaced;
}
ob_start('AutoCache');
clearstatcache();
}
?>