PHP caching mechanisms that must be understood

Source: Internet
Author: User
All PHP programmers know that echo "1" is executed in the PHP script, and "1" is displayed in the guest's browser.
However, when we execute the code below, we do not display "2" after "1" for 5 seconds, but wait 5 seconds to display "12" directly.

echo ' 1 '; sleep (5); Echo ' 2 ';

This involves a few caching mechanisms, for a higher salary, students are very necessary to this caching mechanism to learn well.
Typically, our web application consists of the following elements:
Php->apache-> Browser. In this article, we will use this architecture as an example to explain how the data flows through the "chain".

This involves a few caching mechanisms, for a higher salary, students are very necessary to this caching mechanism to learn well.
Typically, our web application consists of the following elements:
Php->apache-> Browser. In this article, we will use this architecture as an example to explain how the data flows through the "chain".

echo ' 1 '; Ob_flush (); Writes the PHP cache to the APAHCE cache flush (); Write the APAHCE cache to the browser cache sleep (5); Echo ' 2 ';

After we change the code to the above, the browser still has to wait 5 seconds to display "12", because "1" has been sent to the browser, but the browser cache is not full, and no rendering, until the end of the program to render "12."
We take the Google browser as an example (cache 1000bytes), through the following code, we can achieve the first display "1", 5 Seconds to display "2"

Echo str_repeat (', 1000); This will fill the browser cache with echo ' 1 '; Ob_flush (); Writes the PHP cache to the APAHCE cache flush (); Write the APAHCE cache to the browser cache sleep (5); Echo ' 2 ';

In this case, we have to mention the "Ob_start ()" function, the function of which is to open a new PHP cache, but this cache far more than the 4096,php document describes that this cache is large enough. We still use the code to illustrate

Ob_start (); Echo str_repeat (', 1000); This will fill the browser cache with echo ' 1 '; Ob_flush (); Flush (); Write the APAHCE cache to the browser cache sleep (5); Echo ' 2 ';

On the basis of the original we only added a Ob_start (), the result turned into wait 5 seconds after the simultaneous display of "12". This is because each ob_start () in the original cache space to open up a sub-cache space, Ob_flush () is the current cache space output to the upper cache space, PHP only a cache space, the upper cache space is Apache cache, When PHP has more than one cache space, Ob_flush () cannot write the PHP cache to the Apache cache. We still use the image to understand:

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.