悠悠楠杉
网站页面
<?php
start(array(
'index_file' => __DIR__ . '/2.php', //你首页的文件名
'cache_file' => __DIR__ . '/cache/index/', //要缓存的路径
'cache_index_file' => 'index.html', //要缓存的文件名
'expire_seconds' => 43200, //过期的秒数(60秒=1分钟)
));
function start($config)
{
$remain_seconds = $file_timestamp = 0;
if (hasCached($config, $remain_seconds, $file_timestamp)) {
$html = '';
$html .= file_get_contents($config['cache_file'] . $config['cache_index_file']);
die($html);
}
ob_start();
require $config['index_file'];
$content = ob_get_contents();
file_put_contents($config['cache_file'] . $config['cache_index_file'], $content);
}
function hasCached($config, &$remain_seconds, &$file_timestamp)
{
if (!file_exists($config['cache_file'] . $config['cache_index_file'])) {
if (!is_dir($config['cache_file'])) {
mkdir($config['cache_file'], 0777, true); //创建目录/cache/index/
}
fopen($config['cache_file'] . $config['cache_index_file'], 'w'); // 创建文件index.html
return false;
}
if (filesize($config['cache_file'] . $config['cache_index_file']) === 0) {
return false;
}
$file_timestamp = filemtime($config['cache_file'] . $config['cache_index_file']);
$remain_seconds = $config['expire_seconds'] - (time() - $file_timestamp);
if ($remain_seconds <= 0) {
return false;
}
return true;
}