Websites generally need to deal with databases. when the traffic volume is large, it will put a lot of pressure on the database, achieve static PHP pages, and cache the execution results of some dynamic pages, during the next visit, the content displayed on some pages may change every time they are accessed. in this way, websites generally need to deal with databases, when the traffic volume is large, it will put a lot of pressure on the database. Cache the execution results of some dynamic pages. direct access to the cache next time reduces the pressure on the database. At the same time, it can speed up server response. The cached results can be stored in external storage or memory. When you access this page next time, you can directly read the previous result from the external memory.
"; 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.
| 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);?>