>>  Site Map >>  Forums >>  PHP+MySql

Forum module - topics in forum:



PHP+MySql - Coding mã nguồn mở rât thông dụng để làm web, và hệ thống cơ sỏ dữ liệu rất mạnh duợc ưa chuộng



PHP Caching to Speed up Dynamically Generated Sites

This entire site, like many, is built in PHP. PHP provides the power to simply 'pull' content from an external source, in the case of my site this is flat files but it could just as easily be an MySQL database or an XML file etc..

The downside to this is processing time, each request for one page can trigger multiple database queries, processing of the output, and formatting it for display... This can be quite slow on complex sites (or slower servers)

Ironically, these so-called 'dynamic' sites probably have very little changing content, this page will almost never be updated after the day it is written - yet each time someone requests it the scripts goes and fetches the content, applies various functions and filters to it, then outputs it to you...
Quote: :
This example shows a request for a "News" page on a website, the News changes daily so it makes sense to have it in a database rather than as a static file so it can be easily updated and searched, The News page is a PHP script which does the following;

* Connect to an MySQL Database
* Request 5 most recent news items
* Sort news items from most recent to oldest
* Read a template file and substitute variables for content
* Output the finished page to the user


a basic structure that will cache the output of a page for 5 minutes:
=========================================
Code: :

<?php

      $cachefile = "cache/".$reqfilename.".html";

      $cachetime = 5 * 60; // 5 minutes

      // Serve from the cache if it is younger than $cachetime

      if (file_exists($cachefile) && (time() - $cachetime
         < filemtime($cachefile)))
      {

         include($cachefile);

         echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
         -->\n";

         exit;

      }

      ob_start(); // start the output buffer

?>
     

.. Your usual PHP script and HTML here ...


<?php
       // open the cache file for writing
       $fp = fopen($cachefile, 'w');

       // save the contents of output buffer to the file
       fwrite($fp, ob_get_contents());

      // close the file
        fclose($fp);

      // Send the output to the browser
        ob_end_flush();
?>



An alternative method involves checking to see if the data sources have been modified, this increases the load of each request slightly, because it requires a database connection in the case of DB-based sites, or a query of the file modification time of potentially a few files, it also makes the script slightly more complicated. However, this method prevents unecessary LARGE queries, such as those required to retrieve data for inclusion in a page, and prevents regenerating pages regularly even when nothing has changed. This is the approach used on this site.

All that is involved here is changing the if() clause, for example:
Code: :
<?php
        $cachefile = "cache/".$reqfilename.".html";

        // Serve from the cache if it is the same age or younger than the last
        // modification time of the included file (includes/$reqfilename)

        if (file_exists($cachefile) && (filemtime("includes/".$reqfilename))
           < filemtime($cachefile))) {

           include($cachefile);

           echo "<!-- Cached ".date('H:i', filemtime($cachefile))."
           -->\n";

           exit;
        }

       // start the output buffer
        ob_start();
?>
     

.. Your usual PHP script and HTML here ...


<?php
        // open the cache file for writing
        $fp = fopen($cachefile, 'w');

       // save the contents of output buffer to the file
        fwrite($fp, ob_get_contents());

       // close the file
        fclose($fp);

       // Send the output to the browser
        ob_end_flush();
?>


This could be easily adapted to query a database containing a column for 'datemodified' or something similar.

Where not to use Caching
===================
Caching should not be used for some things, the most obvious being search results, forums etc... where the content has to be up-to-the-minute and changes depending on user's input. It's also advisable to avoid using this method for things like a "Latest News" page, in general dont use it on any page that you wouldn't want the end users browser or proxy to cache.




Search from ALEXA


put your ads here