<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
这将告知客户端在该文件夹及其所有子文件夹中缓存内容7天。
您还可以通过使用appcmd.exe
编辑IIS7元数据库来执行此操作,如下所示:
\Windows\system32\inetsrv\appcmd.exe set config "Default Web Site/folder" -section:system.webServer/staticContent -clientCache.cacheControlMode:UseMaxAge \Windows\system32\inetsrv\appcmd.exe set config "Default Web Site/folder" -section:system.webServer/staticContent -clientCache.cacheControlMaxAge:"7.00:00:00"
杰夫,那不是真的。
您只需在IIS 7管理器UI中选择一个文件夹(例如Images或甚至是默认Web应用程序文件夹),然后单击“HTTP响应标头”。然后,您必须在右窗格中单击“设置常见标头..”,并选择“过期Web内容”。在那里,您可以通过选择“After:”,在文本框中输入“24”,并在组合框中选择“小时”轻松配置最长有效期为24小时。
有关web.config条目的第一段是正确的。我会添加cacheControlCustom属性以将缓存控制标头设置为“public”或其他在这种情况下需要的内容。
当然,您也可以根据需要提供web.config条目(或文件)来实现相同的效果。
编辑:删除了一句令人困惑的话 :)
我使用这个
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="500.00:00:00" />
</staticContent>
使用公共缓存控制头,将静态内容缓存500天。
补充Elmer的答案,因为我的编辑被回滚了。
要使用公共缓存控制头缓存静态内容365天,在IIS中可以进行以下配置:
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
这将转换为这样的标题:
Cache-Control: public,max-age=31536000
请注意,max-age是以秒为单位的增量,由正32位整数表示,如RFC 2616第14.9.3和14.9.4节所述。 这代表了2^31或2,147,483,648秒(超过68年)的最大值。 然而,为了更好地确保客户端和服务器之间的兼容性,我们采用了365天(一年)的推荐最大值。
正如其他答案中提到的,您也可以在站点的web.config上使用这些指令来处理所有静态内容。 或者,您还可以仅针对特定位置的内容使用它(例如,在示例中,“cdn”文件夹中的内容使用30天的公共缓存):
<location path="cdn">
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00"/>
</staticContent>
</system.webServer>
</location>
F5刷新的语义是“请重新加载当前HTML和其直接依赖项”。因此,您应该期望任何由HTML直接引用的图像、CSS和JS资源也将被重新获取。当然,304响应是可以接受的,但F5刷新意味着浏览器将发出请求,而不是依赖于新鲜缓存内容。
相反,尝试简单地导航到其他地方,然后再返回。
在大多数浏览器中,您可以在按住Ctrl键的同时按下F5来强制刷新,跳过304。
有一个简单的方法: 1. 使用网站的web.config文件 2. 在“staticContent”部分中删除特定的fileExtension并添加mimeMap 3. 添加“clientCache”
<configuration>
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<staticContent>
<remove fileExtension=".ipa" />
<remove fileExtension=".apk" />
<mimeMap fileExtension=".ipa" mimeType="application/iphone" />
<mimeMap fileExtension=".apk" mimeType="application/vnd.android.package-archive" />
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="777.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
Cache-Control
(max-age
)头吗? - Jasper