启用静态资源的浏览器缓存 [英] Enable browser caching for static resources

查看:587
本文介绍了启用静态资源的浏览器缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要改善网站性能,我添加以下IIS 7.5中的HTTP标头。

To improve the site performance, I'm adding following http headers in IIS 7.5.

过期孙,2020年3月29日00:00:00 GMT
的Cache-Control 公开

我在网站的虚拟目录添加这些标头图片文件夹中。
当我访问该网站,我看到这个文件夹中的每个图像present;这些响应头为:

I'm adding these headers for images folder in site's virtual directory. When I access the site, I see that for each image present in this folder; those response headers were:

接受-范围:字节
缓存控制:无缓存,无店铺,公共
内容长度:4445
内容类型:图像/ PNG
日期:星期五,2014年6月6日九时18分36秒GMT
ETag的:16874c2af55ecf1:0
到期日:-1,孙,2020年3月29日00:00:00 GMT
最后一次修改:星期三,2014年4月23日13时08分48秒GMT
最大年龄:604800
编译:无缓存
服务器:Microsoft-IIS / 7.5
的X已启动方式:ASP.NET

我需要浏览器从它的缓存,而不是从服务器再次请求,采取这些图像。我应该如何实现呢?

I need browser to take these images from it's cache instead of requesting again from server. How should I achieve it?

推荐答案

您标题显示你添加一个新值,但你需要替换现有的

Your header shows that you add a new value but you need to replace the existing one

Cache-Control:no-cache, no-store,Public
Expires:-1,Sun, 29 Mar 2020 00:00:00 GMT

无缓存,无店铺表示无缓存和 1 表示,内容已经过期

no-cache, no-store stands for no cache and -1 says that the content is already expired.

而不是从code做的,你可以很容易地将它设置在根web.config文件

Instead of doing it from the code you can easily set it in the root web.config file as

...
  <location path="images">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="UseExpires" 
                     httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" /> 
      </staticContent>
    </system.webServer>
  </location>
</configuration>

如果照片是你的目录的名称

where images is a name of your directory

或直接在目标目录中添加专用的web.config文件

or add dedicated web.config file directly in the target directory

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseExpires" 
                   httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" /> 
    </staticContent>
  </system.webServer>
</configuration>

您也可以使用cacheControlMode =UseMaxAge,并设置到期的具体时间

You can also use cacheControlMode="UseMaxAge" and set specific time of expiration

举例设置过期7天

<clientCache cacheControlMode="UseMaxAge" 
             cacheControlMaxAge="7.00:00:00" /> 

了解更多 http://msdn.microsoft.com/en-us/库/ ms689443.aspx

这篇关于启用静态资源的浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆