PHP page Set cache time instance code

Source: Internet
Author: User
This article mainly and everybody introduced the PHP processing static page: the page sets the cache time instance, the small compilation thought quite good, now shares to everybody, also gives everybody to make a reference. Follow the small series together to see it, hope to help everyone.

Q: How do I trigger a system to generate a purely static page?

1. Page Add cache time

2. How to trigger manually

3.crontab timed Scan Program

Let's implement scenario one: Add cache time to page

User Request page =/page is out of date = = no (get static page) | | = = (Dynamic page generates a new static page)


if (if there is this static file && not expired) {  //Get page}else{  //Regenerate a copy of static page}

OK, the basic logic is this, the following we refine the code:


<?phpif (Is_file ('./index.html ') && (Time ()-filemtime ('./index.html ') <) {   //assuming the cache is 60 seconds//  Get page  require_once ('./index.html ');} else{  //Regenerate a copy  of the static page//prepare the data to be displayed to the Web  $data = Array (     ' id ' =>1, ' msg ' = ' Hello java '),    Array (' ID ' =>2, ' msg ' = ' Hello php '),    array (' ID ' =>3, ' msg ' = ' Hello python '),  );  Render to template  //actual project is usually rendered in HTML//  demo here want to read  Ob_start ();//Start Input buffer control  foreach ($data as $item) {    echo $item [' id ']. ' ===> '. $item [' msg ']. ' <br/> ';  }  Start generating a static paging file  file_put_contents (' index.html ', ob_get_contents ());}

This way we access index.php, if the static file cache does not expire, its substance accesses the content from index.html this static file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.