提到PHP的內置緩存,我們第一印象應該就是ob_xxx這些函數,的確,php提供非常優秀的內置緩存函數,讓我們能夠實現頁面的靜態化編程,提高我們的網站訪問速度,讓我們網站能夠適應更高并發的業務場景!
:-: 
1、首先有四個函數
ob_start() 開啟緩存
ob_get_contents() 獲取緩沖區的內容
ob_clean() 刪除緩沖區的內容
ob_get_clean() 先獲取然后再刪除緩沖區內容
2、然后我們定義一個函數來生成靜態文件
~~~
/**定義一個緩存文件
* @author crazy
* @time 2018-03-14
*/
public function createCache(){
$action = ACTION_NAME;
$c_name = CONTROLLER_NAME;
$dir = './Cache/'.$c_name.'/'.$action.'/';
if(is_dir($dir)){
file_put_contents("$dir$action".'.shtml',ob_get_contents());
}else{
if(mkdir($dir,0777,true)){
file_put_contents("$dir$action".'.shtml',ob_get_contents());
}
}
}
~~~
3、根據文件目錄是否存在然后我們做相應的重定向
~~~
$action = ACTION_NAME; //thinkphp的常量,功能是依托thinkphp進行示例
$c_name = CONTROLLER_NAME;
$dir = './Cache/'.$c_name.'/'.$action.'/'.$action.'.shtml';
if(file_exists($dir)){
header("Location:http://localhost/simengphp/$dir");
}
~~~
4、局部靜態化
~~~
$.ajax({
url:'',
type:'get',
dataType:'json',
error: function () {
},
success:function(data){
$.each(data.result,function(key,val){
})
}
});
~~~
我們在我們的模板里面寫上這個ajax獲取頁面內容的方法,然后我們調用這個頁面的時候這個方法就會自動的創建了
# living example one
~~~
ob_start();
for($i=0;$i<10;$i++){
echo $i;
}
$output = ob_get_content();
ob_end_clean();
echo $output;
//output:0123456789
~~~
考點
1)ob緩存函數的使用
分析:
第一行ob_start 開啟緩存緩沖區, ob_get_contents()獲取緩沖區的內容,ob_end_clean(),清除并關閉,這時候在清除之前你已經獲取然后并保存在了一個變量里面了,那么將輸出0123456789
:-: 
:-: 