Home >Backend Development >PHP Tutorial >Static implementation of PHP pages_PHP tutorial

Static implementation of PHP pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:041193browse

Nowadays websites generally need to deal with databases. When the number of visits is large, it will put a lot of pressure on the database. Cache the execution results of some dynamic pages. When accessing next time, directly accessing the cache can reduce the pressure on the database. It can also speed up the server's response speed. The cached results can be stored in external memory or in memory. The last result is directly read from the external memory (memory) the next time the page is accessed.


[php]
       
//Check the cache file first
If(file_exists("static.html")){
            //The cache time is 3 minutes
If(time()-filemtime("static.html")<60*3){
//Return the static file content to the client
               $start_time = microtime();
Echo "I read the data read from static files:". "& Lt; br/& gt;";
                  echo file_get_contents("static.html");
                                                                                                                                                                      $end_time   = microtime();                    echo "Static file usage time:".($end_time-$start_time);
exit;
         } 
}  
//If it is the first access, or the last cache time is more than 3 minutes, read the data from the database
$host = "127.0.0.1";
$user = "root";
$password = "123456";
//Recording 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 read the data from the database:
";
Ob_start();//Open the output buffer
echo "

";
//Output the obtained information
While($userInfo = mysql_fetch_assoc($resource)){
echo "";
echo "";
echo "";
echo "";
echo "";
}  
$end_time=microtime();
$str=ob_get_contents();//Get the contents of the buffer
Ob_end_flush();
echo "Time to read data from the database:".($end_time-$start_time);
File_put_contents("static.html",$str);
?>


//Check the cache file first
if(file_exists("static.html")){
//The cache time is 3 minutes
if(time()-filemtime("static.html")<60*3){
//Return the static file content to the client
$start_time = microtime();
echo "I read the data from a static file:"."
";
echo file_get_contents("static.html");
$end_time = microtime();
echo "Static file usage time:".($end_time-$start_time);
exit;
}
}
//If it is the first access, or the last cache time is more than 3 minutes, read the data from the database
$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 read the data from the database:
";
ob_start();//Open output buffer
echo "

NameAddressEmail
".$userInfo['name']."".$userInfo['address']."".$userInfo['email']."
";
//Output the obtained information
while($userInfo = mysql_fetch_assoc($resource)){
echo "";
echo "";
echo "";
echo "";
echo "";
}
$end_time=microtime();
$str=ob_get_contents();//Get the contents of the buffer
ob_end_flush();
echo "Time to read data from the 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:

The average execution time of reading data from the database is: about 0.0008041s

The average execution time of directly reading cached files is: 0.0000475

There are only three records in the database, and SQL is also a simple single-table query. When there are many records in the table, or multi-table queries, the execution time will be longer. Although caching can reduce the number of database accesses and speed up response times, caching is not suitable for all pages. The displayed content of some pages may change each time they are accessed. Such pages obviously cannot use caching. Caching is more suitable for pages that change rarely.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477555.htmlTechArticleNowadays websites generally need to deal with databases. When the number of visits is large, it will put a lot of pressure on the database. . Cache the results of some dynamic page executions and use them next time...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
NameAddressEmail
".$userInfo['name']."".$userInfo['address']."".$userInfo['email']."