Php ob_start () ob_end_flush () simple application of Cache Technology

Source: Internet
Author: User

This article introduces a simple entry to php-simple application of cache technology. If you need it, you can check it out. Here we use ob_start (); ob_end_flush ();.

The Code is as follows: Copy code

<? Php
// Define the path and name of cached file
$ Cachefile = 'cached-files/'. date ('m-d-y').'. php ';
// Define how long we want to keep the file in seconds. I set mine to 5 hours.
$ Cachetime = 18000;
// Check if the cached file is still fresh. If it is, serve it up and exit.
If (file_exists ($ cachefile) & time ()-$ cachetime <filemtime ($ cachefile )){
Include ($ cachefile );
Exit;
}
// If there is either no file OR the file to too old, render the page and capture the HTML.
Ob_start ();
?>
<Html>
Output all your html here.
</Html>
<? Php
// We're done! Save the cached content to a file
$ Fp = fopen ($ cachefile, 'w ');
Fwrite ($ fp, ob_get_contents ());
Fclose ($ fp );
// Finally send browser output
Ob_end_flush ();
?>

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.