启用浏览器缓存的好处不用多说,今天在使用 Google Page Speed 检测网站性能时发现小站还没有开启浏览器缓存,Page Speed 中提示 Leverage browser caching,大概意思就是发挥浏览器缓存的作用。当我在 IIS7 中设置完缓存后,网页速度得分从88分一下到了92分,如下图:
要缓存的静态内容包括图片、html、js、CSS文件,缓存这些文件可明显的提高页面的加载速度。在 (Asp.net / IIS7)中开启浏览器缓存,我们在 web.config 里简单的配置下即可完成,方法为在 system.webServer 节点下添加配置,如下:
<staticContent>
<clientCache cacheControlMaxAge ="168.00:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
以上配置即可缓存静态文件1周,我们也可指定缓存的过期时间,如下:
<staticContent>
<clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
这样将缓存过期时间设置为2020年了,解决方案来自 stackoverflow.com (这个网站确实不错)。本篇内容就这些,希望对遇到这样问题的朋友能带来一些帮助。