PHP uses file storage to implement caching

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

    1. Page business logic processing, getting results
    2. $objPage = new Page_indexmodel ($arrParams);
    3. A series of business logic is placed in the objpage, calling the process method to get the result set
    4. $arrResult = $objPage->process ();
    5. Smarty assignment after getting results
    6. $smarty->assign ($arrResult);
    7. Output template
    8. $smarty->display ();
    9. ?>



Now we use the file cache to skip page business processing this step

[PHP] view plain copy

    1. $cachFile = './index.php ';
    2. The cache file exists for less than an hour, and the cached result set is used directly, and no MySQL queries are made
    3. if (file_exists ($cacheFile) && time ()-filemtime ($cachFile) < 3600) {
    4. Using the results in the cache
    5. $arrResult = include ($cachFile);
    6. } else {
    7. $objPage = new Page_indexmodel ($arrParams);
    8. $arrResult = $objPage->process ();
    9. $strContent = "
    10. Cache the result set
    11. File_put_contents ($cachFile, $strContent);
    12. }
    13. Smarty assignment after getting results
    14. $smarty->assign ($arrResult);
    15. Output template
    16. $smarty->display ();



Reference Source:
PHP uses file storage to implement caching
Http://www.lai18.com/content/407149.html

  • Related Article

    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.