帝国CMS PHP缓存网页代码加速访问
-
定时更新缓存页功能
- 创建一个PHP文件,例如
timeCache.php
,内容如下:<?php define('CACHE_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacheFile'); define('CACHE_TIME', 86400); define('CACHE_FIX', '.php'); date_default_timezone_set("Asia/Shanghai"); $CacheName = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) . CACHE_FIX; $CacheDir = CACHE_ROOT . DIRECTORY_SEPARATOR . substr($CacheName, 0, 1); $CacheUrl = $CacheDir . DIRECTORY_SEPARATOR . $CacheName; if (file_exists($CacheUrl) && time() - filemtime($CacheUrl) < CACHE_TIME && date('H:i', time()) > '06:30') { echo gzuncompress(file_get_contents($CacheUrl)); exit; } elseif (!file_exists($CacheDir)) { if (!file_exists(CACHE_ROOT)) { mkdir(CACHE_ROOT, 0777); chmod(CACHE_ROOT, 0777); } mkdir($CacheDir, 0777); chmod($CacheDir, 0777); } function AutoCache($contents) { global $CacheUrl; $fp = fopen($CacheUrl, 'wb'); $contents = "<!-- 缓存时间 " . (date("Y-m-d H:i:s", time())) . " -->\n" . $contents; fwrite($fp, gzcompress($contents)); fclose($fp); chmod($CacheUrl, 0777); return $contents; } ob_start('AutoCache'); clearstatcache();
- 创建一个PHP文件,例如
-
定期更新页面
- 创建一个PHP文件,例如
cache.php
,内容如下:<?php define('CACHE_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacheFile'); define('CACHE_TIME', 172800); define('CACHE_FIX', '.php'); date_default_timezone_set("Asia/Shanghai"); $CacheName = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) . CACHE_FIX; $CacheDir = CACHE_ROOT . DIRECTORY_SEPARATOR . substr($CacheName, 0, 1); $CacheUrl = $CacheDir . DIRECTORY_SEPARATOR . $CacheName; if (file_exists($CacheUrl) && time() - filemtime($CacheUrl) < CACHE_TIME) { echo gzuncompress(file_get_contents($CacheUrl)); exit; } elseif (!file_exists($CacheDir)) { if (!file_exists(CACHE_ROOT)) { mkdir(CACHE_ROOT, 0777); chmod(CACHE_ROOT, 0777); } mkdir($CacheDir, 0777); chmod($CacheDir, 0777); } function AutoCache($contents) { global $CacheUrl; $fp = fopen($CacheUrl, 'wb'); $contents = "<!-- 缓存时间 " . (date("Y-m-d H:i:s", time())) . " -->\n" . $contents; fwrite($fp, gzcompress($contents)); fclose($fp); chmod($CacheUrl, 0777); return $contents; } ob_start('AutoCache'); clearstatcache();
- 创建一个PHP文件,例如
-
长期永久缓存
- 创建一个PHP文件,例如
content.php
,内容如下:<?php define('CACHE_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cacheFile'); define('CACHE_FIX', '.php'); date_default_timezone_set("Asia/Shanghai"); $CacheName = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) . CACHE_FIX; $CacheDir = CACHE_ROOT . DIRECTORY_SEPARATOR . substr($CacheName, 0, 1); $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; } elseif (!file_exists($CacheDir)) { if (!file_exists(CACHE_ROOT)) { mkdir(CACHE_ROOT, 0777); chmod(CACHE_ROOT, 0777); } mkdir($CacheDir, 0777); chmod($CacheDir, 0777); } function AutoCache($contents) { global $CacheUrl; $fp = fopen($CacheUrl, 'wb'); $contents = "<!-- 缓存时间 " . (date("Y-m-d H:i:s", time())) . " -->\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文件,例如