How to implement caching in PHP based on file storage, _PHP tutorial

WBOY
Release: 2016-07-13 09:46:02
Original
984 people have browsed it

How PHP implements caching based on file storage,

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.

<&#63;php
//页面业务逻辑处理,获得结果
$objPage = new Page_IndexModel($arrParams);
//一系列的业务逻辑放在了objPage中,调用process方法获得结果集
$arrResult = $objPage->process();
//获得结果后smarty赋值
$smarty->assign($arrResult);
//输出模板
$smarty->display();
&#63;>

Copy after login

Now we use file caching to skip the Page business processing step

<&#63;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 = "<&#63;php \n return ".var_export($arrResult, true)."\n;";
  //将结果集缓存
  file_put_contents($cachFile, $strContent);
}
//获得结果后smarty赋值
$smarty->assign($arrResult);
//输出模板
$smarty->display();

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1035666.htmlTechArticlePHP’s method of caching based on file storage. This article describes the method of PHP caching based on file storage. Share it with everyone for your reference. The details are as follows: In some databases...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!