博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浏览器缓存系列之三:设置浏览器缓存
阅读量:4516 次
发布时间:2019-06-08

本文共 6267 字,大约阅读时间需要 20 分钟。

设置浏览器缓存

304是协商缓存还是要和服务器通信一次,要想断绝服务器通信,就要强制浏览器使用本地缓存(cache-control/expires),

一般有如下几种方式设置浏览器缓存。

1、通过HTTP的META设置expires和cache-control

  1. <metahttp-equiv="Cache-Control"content="max-age=7200"/>
  2. <metahttp-equiv="Expires"content="Sun Oct 15 2017 20:39:53 GMT+0800 (CST)"/>

这样写的话仅对该网页有效,对网页中的图片或其他请求无效。 2、apache服务器配置图片,css,js,flash的缓存 这个主要通过服务器的配置来实现这个技术,如果使用apache服务器的话,可以使用mod_expires模块来实现:

编译mod_expires模块:

  1. Cd/root/httpd-2.2.3/modules/metadata
  2. /usr/local/apache/bin/apxs -i -a -c mod_expires.c //编译

先打开httpd.conf文件,然后查找expires这个模块,找到后,删除左边的#号,表示打这个模块,并重启apache服务器

编辑httpd.conf配置:添加下面内容

  1. <IfModulemod_expires.c>
  2. ExpiresActive on
  3. ExpiresDefault "access plus 1 month"
  4. ExpiresByType text/html "access plus 1 months"
  5. ExpiresByType text/css "access plus 1 months"
  6. ExpiresByType image/gif "access plus 1 months"
  7. ExpiresByType image/jpeg "access plus 1 months"
  8. ExpiresByType image/jpg "access plus 1 months"
  9. ExpiresByType image/png "access plus 1 months"
  10. EXpiresByType application/x-shockwave-flash "access plus 1 months"
  11. EXpiresByType application/x-javascript "access plus 1 months"
  12. #ExpiresByType video/x-flv "access plus 1 months"
  13. </IfModule>

3、php等设置

  1. <?php
  2. header("Cache-Control: public");
  3. header("Pragma: cache");
  4. $offset =30*60*60*24;// cache 1 month
  5. $ExpStr ="Expires: ".gmdate("D, d M Y H:i:s", time()+ $offset)." GMT";
  6. header($ExpStr);
  7. ?>

或者

  1. $seconds_to_cache =3600;
  2. $ts = gmdate("D, d M Y H:i:s", time()+ $seconds_to_cache)." GMT";
  3. header("Expires: $ts"); header("Pragma: cache");
  4. header("Cache-Control: max-age=$seconds_to_cache");

4、tomcat中设置max-age或expires

首先pom.xml需要引入catalina包,如果不是使用的maven,请自行搜索下载jar包

  1. <dependency>
  2. <groupId>org.apache.tomcat</groupId>
  3. <artifactId>tomcat-catalina</artifactId>
  4. <version>7.0.61</version>
  5. </dependency>

注意,版本必须是7.0.61以上的,如果你不是maven需要引入jar包及相关的依赖包。

其次,然后找到你j2ee项目中的web.xml文件,在文件中加入如下内容

  1. <filter>
  2. <filter-name>ExpiresFilter</filter-name>
  3. <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
  4. <init-param>
  5. <param-name>ExpiresByType text/html</param-name>
  6. <param-value>access plus 1 minutes</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>ExpiresByType image</param-name>
  10. <param-value>access plus 10 years</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>ExpiresByType text/css</param-name>
  14. <param-value>access plus 10 months</param-value>
  15. </init-param>
  16. <init-param>
  17. <param-name>ExpiresByType application/javascript</param-name>
  18. <param-value>access plus 10 months</param-value>
  19. </init-param>
  20. <init-param>
  21. <param-name>ExpiresByType application/x-unknown-content-type</param-name>
  22. <param-value>access plus 10 years</param-value>
  23. </init-param>
  24. </filter>
  25. <filter-mapping>
  26. <filter-name>ExpiresFilter</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. <dispatcher>REQUEST</dispatcher>
  29. </filter-mapping>

以上内容分别对js脚本、css样式、图片以及html页面进行了缓存设置。

其中param-value的值可以设置为比如access plus 1 month 15 days 2 hours

不可以使用以下的任意的类型或类型组合,(这个我没看懂!~)

years months weeks days hours minutes seconds

PS:再次提醒catalina的版本要7.0.61以上的才行,低版本里未实现filters.ExpiresFilter类。

5、nginx设置max-age或expires 在server节点下加入如下代码

  1. location ~* \.(gif|jpg|png|bmp)$ {
  2. expires 10d;
  3. }

这里是设置图片的过期时间为10天。如果你的图片基本不更新可以设置的时间长一些。

设置浏览器缓存

304是协商缓存还是要和服务器通信一次,要想断绝服务器通信,就要强制浏览器使用本地缓存(cache-control/expires),

一般有如下几种方式设置浏览器缓存。

1、通过HTTP的META设置expires和cache-control

  1. <metahttp-equiv="Cache-Control"content="max-age=7200"/>
  2. <metahttp-equiv="Expires"content="Sun Oct 15 2017 20:39:53 GMT+0800 (CST)"/>

这样写的话仅对该网页有效,对网页中的图片或其他请求无效。 2、apache服务器配置图片,css,js,flash的缓存 这个主要通过服务器的配置来实现这个技术,如果使用apache服务器的话,可以使用mod_expires模块来实现:

编译mod_expires模块:

  1. Cd/root/httpd-2.2.3/modules/metadata
  2. /usr/local/apache/bin/apxs -i -a -c mod_expires.c //编译

先打开httpd.conf文件,然后查找expires这个模块,找到后,删除左边的#号,表示打这个模块,并重启apache服务器

编辑httpd.conf配置:添加下面内容

  1. <IfModulemod_expires.c>
  2. ExpiresActive on
  3. ExpiresDefault "access plus 1 month"
  4. ExpiresByType text/html "access plus 1 months"
  5. ExpiresByType text/css "access plus 1 months"
  6. ExpiresByType image/gif "access plus 1 months"
  7. ExpiresByType image/jpeg "access plus 1 months"
  8. ExpiresByType image/jpg "access plus 1 months"
  9. ExpiresByType image/png "access plus 1 months"
  10. EXpiresByType application/x-shockwave-flash "access plus 1 months"
  11. EXpiresByType application/x-javascript "access plus 1 months"
  12. #ExpiresByType video/x-flv "access plus 1 months"
  13. </IfModule>

3、php等设置

  1. <?php
  2. header("Cache-Control: public");
  3. header("Pragma: cache");
  4. $offset =30*60*60*24;// cache 1 month
  5. $ExpStr ="Expires: ".gmdate("D, d M Y H:i:s", time()+ $offset)." GMT";
  6. header($ExpStr);
  7. ?>

或者

  1. $seconds_to_cache =3600;
  2. $ts = gmdate("D, d M Y H:i:s", time()+ $seconds_to_cache)." GMT";
  3. header("Expires: $ts"); header("Pragma: cache");
  4. header("Cache-Control: max-age=$seconds_to_cache");

4、tomcat中设置max-age或expires

首先pom.xml需要引入catalina包,如果不是使用的maven,请自行搜索下载jar包

  1. <dependency>
  2. <groupId>org.apache.tomcat</groupId>
  3. <artifactId>tomcat-catalina</artifactId>
  4. <version>7.0.61</version>
  5. </dependency>

注意,版本必须是7.0.61以上的,如果你不是maven需要引入jar包及相关的依赖包。

其次,然后找到你j2ee项目中的web.xml文件,在文件中加入如下内容

  1. <filter>
  2. <filter-name>ExpiresFilter</filter-name>
  3. <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
  4. <init-param>
  5. <param-name>ExpiresByType text/html</param-name>
  6. <param-value>access plus 1 minutes</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>ExpiresByType image</param-name>
  10. <param-value>access plus 10 years</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>ExpiresByType text/css</param-name>
  14. <param-value>access plus 10 months</param-value>
  15. </init-param>
  16. <init-param>
  17. <param-name>ExpiresByType application/javascript</param-name>
  18. <param-value>access plus 10 months</param-value>
  19. </init-param>
  20. <init-param>
  21. <param-name>ExpiresByType application/x-unknown-content-type</param-name>
  22. <param-value>access plus 10 years</param-value>
  23. </init-param>
  24. </filter>
  25. <filter-mapping>
  26. <filter-name>ExpiresFilter</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. <dispatcher>REQUEST</dispatcher>
  29. </filter-mapping>

以上内容分别对js脚本、css样式、图片以及html页面进行了缓存设置。

其中param-value的值可以设置为比如access plus 1 month 15 days 2 hours

不可以使用以下的任意的类型或类型组合,(这个我没看懂!~)

years months weeks days hours minutes seconds

PS:再次提醒catalina的版本要7.0.61以上的才行,低版本里未实现filters.ExpiresFilter类。

5、nginx设置max-age或expires 在server节点下加入如下代码

  1. location ~* \.(gif|jpg|png|bmp)$ {
  2. expires 10d;
  3. }

这里是设置图片的过期时间为10天。如果你的图片基本不更新可以设置的时间长一些。

转载于:https://www.cnblogs.com/myprogramer/p/10310713.html

你可能感兴趣的文章
ADO.NET类的模型关系图
查看>>
SRM 604 DIV2 250
查看>>
python中异常处理之esle,except,else
查看>>
看苹果官方API
查看>>
06-基础-系统指令-v-model-语法糖原理
查看>>
论文网站相关链接
查看>>
ipad4自动下载了ios8的安装包,好几个G啊,不想更新,怎么删了呢?
查看>>
JS中的Navigator 对象
查看>>
Android 开源控件与常用开发框架开发工具类
查看>>
记录一次网站打开卡--排故障过程
查看>>
第四章小结
查看>>
Windows7下python2.7.6环境变量配置
查看>>
java设计模式------代理模式
查看>>
WPF学习笔记----注释标记,使用自定义资源字典(style)文件的流程
查看>>
元素定位的八大法则
查看>>
Sublime Text 3 使用小记
查看>>
总结Pycharm里面常用的快捷键
查看>>
util.promisify 的那些事儿
查看>>
配置phpstudy+phpstorm+xdebug环境
查看>>
BZOJ 1079 [SCOI2008]着色方案
查看>>