PHP uses file storage to implement caching
In some database data record large, but the server is limited, maybe a MySQL query will be good hundreds of milliseconds, a simple page generally also has more than 10 queries, this time also a page load down basically several seconds, if the concurrency is high, the server basically paralyzed, Cause a page for a long time also load not down, this time we can use the file cache to alleviate the pressure under MySQL, the following is a use example.
[PHP] view plain copy
-
- Page business logic processing, getting results
- $objPage = new Page_indexmodel ($arrParams);
- A series of business logic is placed in the objpage, calling the process method to get the result set
- $arrResult = $objPage->process ();
- Smarty assignment after getting results
- $smarty->assign ($arrResult);
- Output template
- $smarty->display ();
- ?>
Now we use the file cache to skip page business processing this step
[PHP] view plain copy
-
- $cachFile = './index.php ';
- The cache file exists for less than an hour, and the cached result set is used directly, and no MySQL queries are made
- if (file_exists ($cacheFile) && time ()-filemtime ($cachFile) < 3600) {
- Using the results in the cache
- $arrResult = include ($cachFile);
- } else {
- $objPage = new Page_indexmodel ($arrParams);
- $arrResult = $objPage->process ();
- $strContent = "
- Cache the result set
- File_put_contents ($cachFile, $strContent);
- }
- Smarty assignment after getting results
- $smarty->assign ($arrResult);
- Output template
- $smarty->display ();
Reference Source:
PHP uses file storage to implement caching
Http://www.lai18.com/content/407149.html