Today's websites generally need to deal with databases, which puts a lot of pressure on the database when the traffic is large. Cache the execution results of some dynamic pages. direct access to the cache next time reduces the pressure on the database. It can also speed up server response... "/> <scripttype =" text/javascript "src =" http://www.2cto.c
Today's websites generally need to deal with databases, which puts a lot of pressure on the database when the traffic is large. 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.
[Php]
// 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 "I read 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 exceeds 3 minutes, the data will be read 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 data from the database:
";
Ob_start (); // enable the output buffer.
Echo"
// 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 "I read 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 exceeds 3 minutes, the data will be read 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 data from the database:
";
Ob_start (); // enable the output buffer.
Echo"
| Name |
Address |
Email |
";// Output the obtained informationWhile ($ 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 contentOb_end_flush ();Echo "time when data is read from the database:". ($ end_time-$ start_time );File_put_contents ("static.html", $ str );?>
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 informationWhile ($ 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 contentOb_end_flush ();Echo "time when data is read from the database:". ($ end_time-$ start_time );File_put_contents ("static.html", $ str );?>The users table contains three records using the apache service. The test results are as follows: