PHP Cache based on file storage _ php tips-php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.