在這裡,我們通過一個代碼示例進一步了解PHP自身的緩存機制,在今後的學習中,也會對緩存這個概念有一個大緻的了解。
使用php自身的緩存機制
如果要測試php自己的緩存機制, 需要配置php.ini 檔案display_errors=On
output_buffering=Off
error_reporting= 設定錯誤級别
兩種緩沖區:outputbuffer和程式緩存
ob緩存關閉以後就放到程式緩存,程式緩存必須放到header後面,否則報錯
函數:ob_start(); //開啟緩存
ob_clean(); //清空 outputbuffer的内容
ob_end_clean(); //關閉ob緩存,同時清空
ob_flush(); //輸出ob内容,并清空,但不關閉
ob_end_flush(); //把ob緩存的内容輸出,并關閉ob
ob_get_contents(); //擷取output_buffering的内容
測試題:<?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");
?>
輸出:aa
不報錯
沒有關閉,aa放入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");
?>
輸出:aa
報錯
aa放入的是程式緩存<?php
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_flush();
echo "aa";
echo ob_get_contents();
?>
輸出:abchelloaaaa
等待後面的一起輸出<?php
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_end_flush();
echo "aa";
echo ob_get_contents();
?>
輸出:abchelloaaabchelloaa
最後再關閉
以上就是我整理的PHP自身的緩存機制,希望今後的學習會對大家有所幫助。
相關文章: