Heim  >  Artikel  >  Backend-Entwicklung  >  PHP中实现生成静态文件的方法缓解服务器压力_php技巧

PHP中实现生成静态文件的方法缓解服务器压力_php技巧

WBOY
WBOYOriginal
2016-05-17 08:51:31902Durchsuche

互联网快速普及的现在社会,越来越多的人会在一个web应用上进行交流,因而导致服务器与数据库访问压力与日俱增,这边就需要进行一些优化,譬如增加缓存、二级缓存、动态网页静态化以及其他的高端技术。

此处作者是使用生成静态文件的方法:

复制代码 代码如下:

/**
* @desc 生成静态文件时使用
* @todo htmlcache
* @param $root string html文件存放路径
* @param $rule string html文件命名规则
* @param $url string 需要生成HTML文件的动态网页地址
* @return boole true/false
*/
public function setHtmlCache($root,$rule,$url){
//检测是否生成过静态文件
if (file_exists($root.$rule.'.html')){//已经存在静态文件
@unlink($root.$rule.'.html');//删除静态文件
return file_put_contents($root.$rule.'.html', file_get_contents($url));//重新生成
}else{//不存在静态文件直接生成
return file_put_contents($root.$rule.'.html', file_get_contents($url));
}
}

该段方法是最普通的生成静态文件方式,欢迎大家进行交流沟通!
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn