一、HTTP缓存
1.Last-Modified头
public function behaviors(){
return [
[
'class' => 'yii\filters\HttpCache',
'lastModified =>function(){
return Unix时间戳;
}
]
];
}
2.ETag头
public function behaviors(){
return [
[
'class' => 'yii\filters\HttpCache',
'only' =>['testview']<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
'etagSeed=>function(){
return 字符串;
}
]
];
}
例子:
public function behaviors(){
return [
[
'class'=>'yii\filters\HttpCache',
'lastModified'=>function(){
return filemtime("hw.txt"); //该文件<span style="color: rgb(51, 51, 51); font-family: arial; font-size: 13px; line-height: 20.0200004577637px;">最近一次被修改的时间</span>
},
'etagSeed' =>function(){
$fp = fopen('hw.txt','r');
$title = fgets($fp); //获取文件第一行文字以此作为eta的值,若修改则缓存失效
fclose($fp);
return $title;
}
]
];
}
lastModified 倾向于以最后一次的修改时间来进行判断
etagSeed 通过对比客户端的哈希值和服务器端生成的哈希值,浏览器就能判断页面是否被修改过