Use the built-in PHP function to output the cache. If your website's MySQL database is slow, you need to pay attention to the website's cache. Anyone who has used WordPress knows that it has a plug-in called WPSuperCache, which can take the website cache seriously if the MySQL database on your website is slow. Anyone who has used WordPress knows that it has a plug-in called WP Super Cache, which can store WordPress pages as static pages when they are generated for the first time. when you request this page again, it saves the time to read the database. This technology is discussed here.
The first question is how to get the PHP output content. The reason for getting the output content is very simple, because we can store the output content and give it to the visitor when they come again.
It is also easy to achieve these goals. We only need to call the function ob_start () before the content is output, and then call ob_get_contents () after all the content is output, and then call ob_end_flush () an example is as follows:
Output outside the PHP tag can be recorded.
I have been recorded.'?>
Program running result:
Output outside the PHP tag can be recorded. I have been recorded. Output outside the PHP tag can be recorded. I have been recorded.
As you can see, the $ cache variable saves the previous output results. That is to say, we can use cache to reduce PHP result output.
Sometimes we have this habit: for administrators, caching is disabled, while for tourists, caching is enabled. At this time, the implementation is also relatively simple. We can write two functions cache ($ id) and end_cache ($ id) by ourselves, indicating the cache start and cache end, respectively, and the code is as follows (three functions are omitted here ):
Sometimes, the website may create pages specially designed for mobile devices as needed. In this case, we should extend $ id. There are many methods for this extension, such as adding another parameter to make the pages of mobile devices different from the folders of desktop devices, and these pages use the same $ id. another way is to combine the original $ id with the User-agent of the mobile device. md5 () is enough. I prefer the previous approach. Of course, there must be other similar practices. In short, the central idea is to set the cache tag ($ id) to something different and distinguish them when the user returns, you can.
In addition, a website may have multiple user roles, which may be cached by corresponding users. Of course, you only need to follow the above principles.
Ob_start () and ob_end_flush () are recursively processed. That is to say, you can call ob_end_flush () several times before calling ob_end_flush (). For example:
';ob_start();echo 'content2'.'
';$output1 = ob_get_contents(); echo $output1.'
';ob_end_flush();$output2 = ob_get_contents(); echo $output2.'
';ob_end_flush();?>
Program running result:
content1content2content2content1content2content2
$ Output2 caches the first three outputs.
Bytes. Anyone who has used WordPress knows that it has a plug-in called WP Super Cache, which can...