The ob series functions are mainly used in php page caching, such as ob_start(), ob_end_flush(), ob_get_contents(), but more advanced Caching does not use these functions. An example will be given at the end of this article to illustrate.
Let’s first take a look at the ob series functions commonly used for caching:
ob_start(): The sign of the start of page cache. The content under this function is saved in the page cache until ob_end_flush() or ob_end_clean();
ob_get_contents(): used to obtain the content in the page cache. After obtaining it, we can process the content however we want, filter fields, match content, etc.~~~
ob_end_flush(): Indicates the end of page caching, and as verified by me, the cached content will be output to the current page, which means the cached content can be displayed.
Using these three PHP functions, you can achieve powerful functions. If the database query volume is large, you can use cache to solve this problem.
The following is the encoding part.
1. Initialization function, usually setting the page cache path, cache file naming format, etc., which can be customized according to personal preferences. The identification ID used here is the encrypted $_SERVER[REQUEST_URI] parameter. The last part of this function is There is an if judgment. If the cache period has not passed, load the cache file, otherwise load the source file. The code is as follows:
The code is as follows:
2. Page cache function. A trick is used here. Add a header information - expiration time to the content of the cache file, so each time you only need to compare the expiration time in the header with the current time. In page_init( ) function, you can determine whether the cache has expired. The code is as follows:
Copy the code The code is as follows:
3. When using functions, please note that these two functions are executed in sequence, and don’t forget ob_start(). The code is as follows:
The code is as follows:
Example 2. Here is an example to illustrate PHP page caching technology. The code is as follows:
For example, using generated files for caching, the code is as follows:
The code is as follows: