Home >Backend Development >PHP Tutorial >PHP caching techniques through file storage
This article mainly introduces the techniques of PHP caching through file storage. Interested friends can refer to it. I hope it will be helpful to everyone.
The example in this article describes the method of PHP caching based on file storage, as follows:
When some database data records are large, but the server is limited, a MySQL query may be several One hundred milliseconds. A simple page usually has more than a dozen queries. At this time, it takes several seconds to load the page. If the concurrency is high, the server will basically be paralyzed, causing a page to be unable to load for a long time. At this time, we You can use file caching to relieve the pressure on MySQL. Here is an example of use.
<?php //页面业务逻辑处理,获得结果 $objPage = new Page_IndexModel($arrParams); //一系列的业务逻辑放在了objPage中,调用process方法获得结果集 $arrResult = $objPage->process(); //获得结果后smarty赋值 $smarty->assign($arrResult); //输出模板 $smarty->display(); ?>
Now we use file caching to skip the Page business processing step
<?php $cachFile = './index.php'; //缓存文件存在且时间不超过一小时,则直接使用缓存的结果集,不在进行任何的MySQL查询了 if(file_exists($cacheFile) && time()-filemtime($cachFile) < 3600) { //使用缓存中的结果 $arrResult = include($cachFile); } else { $objPage = new Page_IndexModel($arrParams); $arrResult = $objPage->process(); $strContent = "<?php \n return ".var_export($arrResult, true)."\n;"; //将结果集缓存 file_put_contents($cachFile, $strContent); } //获得结果后smarty赋值 $smarty->assign($arrResult); //输出模板 $smarty->display();
Summary: The above is the entire content of this article, I hope it can help everyone learn Helps.
Related recommendations:
About encapsulation and inheritance of PHP classes
php function to obtain the birthday in the ID card and verify whether you are an adult
The above is the detailed content of PHP caching techniques through file storage. For more information, please follow other related articles on the PHP Chinese website!