您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
6-3 http缓存之缓存时机
发布时间:2023-05-11 22:21:18编辑:雪饮阅读()
-
正常响应头
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Thu, 11 May 2023 11:27:17 GMT
Server: nginx/1.15.11
Transfer-Encoding: chunked
X-Powered-By: PHP/5.4.45
配置了http缓存的响应头
Cache-Control: public, max-age=3600
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Thu, 11 May 2023 13:59:34 GMT
Last-Modified: Thu, 28 May 2015 12:52:44 GMT
Pragma
Server: nginx/1.15.11
Set-Cookie: PHPSESSID=b12m57mf3saqistl4nqel3e1k0; path=/; HttpOnly
Transfer-Encoding: chunked
X-Powered-By: PHP/5.4.45
这里简单说下,就是配置了http缓存,其实原理大概就是利用Cache-Control响应头字段,该字段里面的max-age的值好像是缓存的有效期,单位好像是秒。
那么Yii框架如何实现http缓存?
<?php
namespace app\controllers;
use yii\web\Controller;
class HelloController extends Controller{
public function behaviors()
{
return [
[
'class'=>'yii\filters\HttpCache',
'lastModified'=>function(){
return 1432817564;
}
]
];
}
public function actionIndex(){
return $this->renderPartial('62');
}
}
这里主要我觉得就是'lastModified'这个字段,应该返回的也是秒数,或者就是当前时间戳吧(应该不是当前时间戳,应该是旧一些时候的时间戳吧)。
这里这个62视图模板对应如D:\phpstudy_pro\WWW\www.xyyii.com\basic\views\hello\62.php
当然了,这个模板内容就无所谓咯。
然后当你62.php这个里面文件内容改变,你再次请求会发现响应码为304,这样就是http缓存吧。
关键字词:http缓存
相关文章
-
无相关信息