利用浏览器缓存

2022-08-31 01:00:59

根据:http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching 我应该使用浏览器缓存。但是,我不知道该怎么做。

我是否只需将某些标签添加到 html 部分?还是我需要通过服务器发送到客户端的东西?与php头有关?


答案 1

缓存通过各种 HTTP 标头进行控制。您应该阅读Mark Nottingham的面向Web作者和网站管理员的缓存教程。您可以使用 header 函数为从 PHP 输出的文档设置 HTTP 标头


答案 2

您可以在 .htaccess 中执行类似操作。

  ## EXPIRES CACHING ##
<IfModule mod_expires>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
  ## EXPIRES CACHING ##

推荐