PHP page static implementation of _php tutorial

Source: Internet
Author: User
Today's web site generally need to deal with the database, access to a large amount of the database will be a great pressure. Caching the results of some dynamic page execution, and accessing the cache directly during the next visit can reduce the stress on the database. It can also speed up the responsiveness of the server. The cached results can be stored on the external memory or in memory. The last result is read directly from external memory (memory) The next time the page is accessed.


[PHP]

First look at the cache file
if (file_exists ("static.html")) {
Cache time is 3 minutes
if (Time ()-filemtime ("static.html") <60*3) {
Returning static file content to the client
$start _time = Microtime ();
echo "I am reading data from a static file:". "
";
Echo file_get_contents ("static.html");
$end _time = Microtime ();
echo "Static File Usage time:". ($end _time-$start _time);
Exit
}
}
Read data from the database if it is first accessed, or if the last cache time exceeds 3 minutes
$host = "127.0.0.1";
$user = "root";
$password = "123456";
Record start time
$start _time = Microtime ();
Mysql_connect ($host, $user, $password);
mysql_select_db ("MyDB");
mysql_query ("Set names UTF8");

$sql = "Select Name,address,email from Users";
$resource = mysql_query ($sql);
echo "I am the data read from the database:
";
Ob_start ();//Open Output buffer
echo "
















First look at the cache file
if (file_exists ("static.html")) {
Cache time is 3 minutes
if (Time ()-filemtime ("static.html") <60*3) {
Returning static file content to the client
$start _time = Microtime ();
echo "I am reading data from a static file:". "
";
Echo file_get_contents ("static.html");
$end _time = Microtime ();
echo "Static File Usage time:". ($end _time-$start _time);
Exit
}
}
Read data from the database if it is first accessed, or if the last cache time exceeds 3 minutes
$host = "127.0.0.1";
$user = "root";
$password = "123456";
Record start time
$start _time = Microtime ();
Mysql_connect ($host, $user, $password);
mysql_select_db ("MyDB");
mysql_query ("Set names UTF8");

$sql = "Select Name,address,email from Users";
$resource = mysql_query ($sql);
echo "I am the data read from the database:
";
Ob_start ();//Open Output buffer
echo "

"; Output information while ($userInfo = Mysql_fetch_assoc ($resource)) {echo " "; echo " "; echo " "; echo " "; echo " "; } $end _time=microtime (); $str =ob_get_contents ();//Gets the contents of the buffer Ob_end_flush (); echo "Time to read data from database:". ($end _time-$start _time); File_put_contents ("static.html", $str);?>
name Address Email
". $userInfo [' name ']."". $userInfo [' address ']."". $userInfo [' email ']."















Read data from the database its average execution time is: About 0.0008041s

The average execution time for the direct read cache file period is: 0.0000475

There are only three records in the database, and SQL is a simple single-table query, which can take longer to execute when a lot of records are recorded in the table, or a multi-table query. Although caching can reduce the number of times a database is accessed, it accelerates response time, but the cache is not suitable for all pages. Some pages may change the display of their pages each time they visit, so the page obviously cannot use the cache. Caching is more appropriate for pages with few changes.

http://www.bkjia.com/PHPjc/477555.html www.bkjia.com true http://www.bkjia.com/PHPjc/477555.html techarticle today's web site generally need to deal with the database, access to a large amount of the database will be a great pressure. The results of some dynamic page execution are cached when the next time ...

  • "//Output information while ($userInfo = MYSQL_FETCH_ASSOC ($resource)) {echo " "; echo " "; echo " "; echo " "; echo " "; } $end _time=microtime (); $str =ob_get_contents ();//Gets the contents of the buffer Ob_end_flush (); echo "Time to read data from database:". ($end _time-$start _time); File_put_contents ("static.html", $str);? There are three records in the >users table, using the Apache service. The test results are as follows:
    name address Email
    ". $userInfo [' name ']. "". $userInfo [' address ']. "". $userInfo [' email ']. "

    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.