The example in this article describes how PHP implements caching based on file storage. Share it with everyone for your reference. The details are as follows:
When some databases have large data records but limited servers, a MySQL query may take hundreds of milliseconds. A simple page usually has more than a dozen queries. At this time, it will take several seconds to load the page. If the amount of 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 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();
I hope this article will be helpful to everyone’s PHP programming design.