什么是动态语言静态化
将现有的php等动态语言的逻辑代码生成静态html文件, 用户访问动态脚本重定向到静态html文件的过程
对实时性要求不高的页面
为什么要静态化
原因:
动态脚本通常会做逻辑计算和数据查询, 访问量越大, 服务器压力越大
访问量大时可能会造成cpu负载过高, 数据库服务器压力过大
静态化可以减小逻辑处理压力, 降低数据库服务器查询压力
静态化的实现方式
使用模板引擎:
可以使用smarty的缓存机制生成静态html缓存文件
$smarty->cache_dir = $ROOT."/cache"; //缓存目录
$smarty->cachhing = true; //是否开启缓存
$smarty->cache_lifetime = "3600"; //缓存时间
$smarty->display(string template[, string cache_id[, string compile_id]]);
$smarty->clear_all_cache(); //清除所有缓存
$smarty->clear_cache('file.html'); //清除指定的缓存
$smarty->clear_cache('article.html'. $art_id); //清除同一个模版下的指定缓存号的缓存
利用ob系列的函数:
ob_start(): 打开输出控制缓冲
ob_get_contents(): 返回输出缓冲内容
ob_clean(): 清空输出缓冲区
ob_end_flush(): 冲刷出(送出)输出缓冲区内容并关闭缓冲
可以判断文件的inode修改时间, 判断是否过期使用filectime函数
<?php
$id = isset($_GET['id']) ? $_GET['id'] : 0 ;
$cache_name = md5(__FILE__) .'-'. $id .'.html';
$cache_lifetime = 3600;
if( file_exists($cache_name) && filectime($cache_name)+$cache_lifetime > time() && filectime(__FILE__) <= filectime($cache_name) )
{
include $cache_name;
die;
}
ob_start();
?>
<b>This is my script <?= $id ?></b>
<?php
$content = ob_get_contents();
ob_end_flush();
file_put_contents($cache_name, $content);
?>