zhangpengfei00 发表于 2018-12-23 10:28:04

php header cache

  php header()中的cache有四种标识头:Last-Modified,Expires,Pragma: no-cache,Cache-Control。
  以下的测试环境为谷歌浏览器,其余的未测试

[*]  Last-Modified:在判断缓存的时候,If-Modified-Since会将此值发送给服务器。
[*]  Expires:后面的日期用于标识缓存在什么时候过期
[*]  Pragma:尽量用Cache-Control替代
[*]  Cache-Control:可以有如下的值:

[*]  public:可以在任何地方缓存,包括浏览器、代理等
[*]  private:只能被浏览器缓存,共享缓存不可被缓存
[*]  no-cache:不缓存
[*]  no-store:
[*]  no-transform:
[*]  must-revalidate:缓存必须检查更新版本
[*]  proxy-revalidate:代理缓存必须检查更新版本
[*]  max-age:内容能够被缓存的时期,以秒表示
[*]  s-maxage:覆盖共享缓存的max-age设置

  下面看看Last-Modified
  1.php

this is page 1

page 1 number:111
  2.php

this is page2

page 2 number:
  当点击链接或者刷新1.php的时候,会发现其RequestHeaders中多了一行If-Modified-Since字段。但是,单独的Last-Modified不会有任何的缓存作用。
  

  下面来看看Expires:
  将1.php修改

this is page 1

page 1 number:111
  第一次请求1.php的时候,和普通的无差别
  在2.php中跳转回1.php,发现Network--Size中显示的是from cache,而且点击文件,其Headers一栏也没有RequestHeaders等信息。它直接从缓存中获取。此时即使将div中的数字修改,在300秒以内,通过2.php链接跳转回来,依然显示的是未修改前的页面。
  但是刷新页面以后,请求和普通的也无差别。
  

  接下来看看Cache-control
  将1.php修改为

this is page 1

page 1 number:111
  对于没有设置时间的Cache-control,和普通的无差别
  接下来继续修改

this is page 1

page 1 number:111
  这个和通过Expires设置的差不多。
  当页面为

this is page 1

page 1 number:111
  即使不设置private参数,也会缓存
  当参数为no-cache的时候,均会重新请求
  




页: [1]
查看完整版本: php header cache