-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
36 lines (34 loc) · 1.27 KB
/
index.php
File metadata and controls
36 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
start(array(
'index_file' => dirname(__FILE__) . '/default.php', //你首页的文件名
'cache_file' => dirname(__FILE__) . '/cache/index/index.html', //要缓存的文件名
'expire_seconds' => 3600, //过期的秒数(60秒=1分钟)
));
function start($config) {
$remain_seconds = $file_timestamp = 0;
if (hasCached($config, $remain_seconds, $file_timestamp)) {
$html = '';
//$html .= '<!-- [当前为缓存页面,距离更新还有剩余' . $remain_seconds . '秒] -->' . "\r\n";
// $html .= '<!-- [该缓存页面生成于' . date('Y-m-d H:i:s', $file_timestamp) . '] -->' . "\r\n";
$html .= file_get_contents($config['cache_file']);
die($html);
}
ob_start();
include($config['index_file']);
$content = ob_get_contents();
file_put_contents($config['cache_file'], $content);
}
function hasCached($config, &$remain_seconds, &$file_timestamp) {
if (!file_exists($config['cache_file'])) {
return FALSE;
}
if (filesize($config['cache_file']) === 0) {
return FALSE;
}
$file_timestamp = filemtime($config['cache_file']);
$remain_seconds = $config['expire_seconds'] - (time() - $file_timestamp);
if ($remain_seconds <= 0) {
return FALSE;
}
return TRUE;
}