Static PHP pages .? Objects) 60*3) {return the static file content to the client $ start_timemi
// First view the cached file 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 "the data I read from the static file :"."
"; Echo file_get_contents (" static.html "); $ end_time = microtime (); echo" static file usage time :". ($ end_time-$ start_time); exit ;}// if this is the first access or the last cache time exceeds 3 minutes, read 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" data read FROM the database:
"; Ob_start (); // enable the output buffer echo"The users table contains three records using the apache service. The test results are as follows:
The average execution time of reading data from the database is about 0.0008041s.
Average execution time of direct read cache files: 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 query, the execution time will be longer. Although the cache can reduce the number of visits to the database and accelerate the response time, the cache is not suitable for all pages. Some pages may change the content displayed on their pages each time they are accessed. such pages obviously cannot be cached. Cache is suitable for pages with few changes.
Http://www.bkjia.com/PHPjc/371932.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371932.htmlTechArticle? Php // The first-in-first-out slow-saving file if(file_exists(static.html) {// cached) 60*3) {// return the static file content to the client $ start_time = mi...
Name |
Address |
Email |
"; // Output the obtained information while ($ userInfo = mysql_fetch_assoc ($ resource) {echo"
"; Echo"
". $ UserInfo ['name']." | "; Echo"
". $ UserInfo ['address']." | "; Echo"
". $ UserInfo ['email ']." | "; Echo"
";}$ End_time = microtime (); $ str = ob_get_contents (); // Obtain the buffer content ob_end_flush (); echo" time when data is read from the database :". ($ end_time-$ start_time); file_put_contents ("static.html", $ str);?>