Yii之http緩存

public function behaviors(){
    return [
        [
            'class'=>'yii\filters\HttpCache',
            'lastModified' => function(){
                return 23432;//時間戳
            }
        ]
    ];
}
public function actionIndex(){
        return $this->renderPartial('index');
}

另外視圖層:index.php
::
<h1>
    test of httpcache
</h1>

http緩存個人解答:
第一次請求後,結果會緩存在瀏覽器中,請求頭帶上return的時間戳(last-modified),
法①時間對比,last-modified:第二次進行相同請求時,瀏覽器會帶上上一次behaviors中renturn的時間戳,服務器收到後與之比對發現相同,所以返回304狀態碼,告訴瀏覽器調用緩存的記錄就行
法②內容etag與last-modified結合:先驗證last-modified,相同則返回304,不同則驗證etag
結果:
1、第一次請求,瀏覽器輸出:test of httpcache
2、把test of httpcache改爲:second test
3、再次請求,瀏覽器依然輸出test of httpcache

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章