1, to solve the problem.
2. How to implement.
An optimization that frequently accesses the database in the face of high-traffic websites, such as blog websites. It is impossible for everyone to view the database once. In order to solve the problem of a large number of unnecessary visits.
You can save the first content as an html page. Then visit the static page within the defined expiration time.
Here is a small demo
index.php to achieve the main work of static.
1 <?php 2 if(is_file('index.html') && (time() - filemtime('index.html') < 10)) { 3 require_once 'index.html'; 4 } else { 5 date_default_timezone_set('Asia/Shanghai'); 6 $time = date('Y-m-d H:i:s', time()); 7 ob_start(); 8 require_once 'template.php'; 9 file_put_contents('index.html',ob_get_contents()); 10 }
The template.php template file is used to be rendered
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <?php echo $time; ?> 8 </body> 9 </html>