This article mainly introduces PHP's method of implementing cache based on file storage. The example analyzes php's technique of implementing cache through file storage in the smarty template, which has some reference value, for more information, see the following example. Share it with you for your reference. The details are as follows:
When some databases have large data records but the server is limited, a MySQL Query may take several hundred milliseconds. a simple page usually contains more than a dozen queries, at this time, it takes several seconds to load a page. if the concurrency is high, the server will basically be paralyzed, resulting in a page not loading for a long time, at this time, we can use the file cache to relieve the pressure on MySQL. The following is an example.
<? Php // page business logic processing, get the result $ objPage = new Page_IndexModel ($ arrParams); // A series of business logic is placed in objPage, call the process method to obtain the result set $ arrResult = $ objPage-> process (); // after the result is obtained, the smarty value is assigned $ smarty-> assign ($ arrResult ); // output template $ smarty-> display ();?>
Now we use the file cache to skip the Page service processing step.
<? Php $ cachFile = '. /index. php '; // if the cached file exists for less than one hour, the cached result set is directly used, and if (file_exists ($ cacheFile) is not queried in any MySQL) & time ()-filemtime ($ cachFile) <3600) {// use the cached result $ arrResult = include ($ cachFile );} else {$ objPage = new Page_IndexModel ($ arrParams); $ arrResult = $ objPage-> process (); $ strContent = "<? Php \ n return ". var_export ($ arrResult, true ). "\ n;"; // cache the result set file_put_contents ($ cachFile, $ strContent);} // after the result is obtained, the smarty value is $ smarty-> assign ($ arrResult ); // output template $ smarty-> display ();
I hope this article will help you with php programming.