Home > Article > Backend Development > PHP caching mechanism
Here, we use a code example to further understand PHP's own caching mechanism. In future studies, we will also have a general understanding of the concept of caching.
Use php's own caching mechanism
If you want to test php's own caching mechanism, you need to configure the php.ini file
display_errors=On output_buffering=Off error_reporting= 设置错误级别
Two buffers: outputbuffer and program cache
After the ob cache is closed, it is placed in the program cache. The program cache must be placed behind the header, otherwise an error will be reported
Function:
ob_start(); //开启缓存 ob_clean(); //清空 outputbuffer的内容 ob_end_clean(); //关闭ob缓存,同时清空 ob_flush(); //输出ob内容,并清空,但不关闭 ob_end_flush(); //把ob缓存的内容输出,并关闭ob ob_get_contents(); //获取output_buffering的内容
Test Question:
<?php ob_start(); echo "abc"; header("content-type:text/html;charset=utf-8"); echo "hello"; ob_clean(); echo "aa"; header("content-type:text/html;charset=utf-8"); ?>
Output: aa
No error
Not closed, aa is put into ob
<?php ob_start(); echo "abc"; header("content-type:text/html;charset=utf-8"); echo "hello"; ob_end_clean(); echo "aa"; header("content-type:text/html;charset=utf-8"); ?>
Output: aa
Error
aa is put into the program Cache
<?php ob_start(); echo "abc"; header("content-type:text/html;charset=utf-8"); echo "hello"; ob_flush(); echo "aa"; echo ob_get_contents(); ?>
Output: abchelloaaaa
Wait for the next output together
<?php ob_start(); echo "abc"; header("content-type:text/html;charset=utf-8"); echo "hello"; ob_end_flush(); echo "aa"; echo ob_get_contents(); ?>
Output: abchelloaaabchelloaa
Finally close it
The above is the cache of PHP itself that I organized Mechanism, I hope future study will be helpful to everyone.
Related articles:
Must understand the php caching mechanism
Simple php caching class sharing php Cache mechanism_php example
PHP page static study notes three: Use PHP caching mechanism to complete staticization
The above is the detailed content of PHP caching mechanism. For more information, please follow other related articles on the PHP Chinese website!