概述
<profiles>
元素的 <add>
元素将输出缓存配置文件添加到输出缓存配置文件的集合中。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <add> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <add> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <add> 元素在 IIS 8.0 中未进行修改。 |
IIS 7.5 | <add> 元素在 IIS 7.5 中未进行修改。 |
IIS 7.0 | IIS 7.0 中引入了 <profiles> 元素的 <add> 元素。 |
IIS 6.0 | 空值 |
安装
在 IIS 7 的默认安装中包含 <profiles>
元素的 <add>
元素。
操作方式
如何配置页面输出缓存
打开 Internet Information Services (IIS) 管理器:
如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:
- 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows 8 或 Windows 8.1:
- 按住 Windows 键,按字母 X,然后单击“控制面板”。
- 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows Vista 或 Windows 7:
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
在“连接”窗格中,转到要为其配置页面输出缓存的连接、站点、应用程序或目录。
在“操作”窗格中,单击“添加...”。
在“添加缓存规则”对话框中,在“文件扩展名”框中键入要缓存的文件扩展名,然后选择“用户模式缓存”选项和/或“内核模式缓存”选项。
配置
特性
属性 | 说明 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
duration |
可选的 timeSpan 属性。 指定缓存页面或用户控件的时间(以秒为单位)。 默认为 00:00:30 。 |
||||||||||||||
extension |
必需的字符串属性。 指定要缓存的文件的文件扩展名。 |
||||||||||||||
kernelCachePolicy |
可选的枚举属性。 配置内核缓存策略。 kernelCachePolicy 属性可以是以下值之一。 默认为 DontCache 。
|
||||||||||||||
location |
可选的枚举属性。 指定有效值,用于控制资源的输出缓存 HTTP 响应的位置。 location 属性可为以下值之一。 默认为 Server 。
|
||||||||||||||
policy |
可选的枚举属性。 配置输出缓存策略。 policy 属性可以是下列可能值之一。 默认为 DontCache 。
|
||||||||||||||
varyByHeaders |
可选的字符串属性。 指定一个以分号分隔的 HTTP 标头的列表,这些标头用于使输出缓存发生变化。 当此属性设置为多个标头时,输出缓存包含所请求文档的不同版本,用于指定标头的各种组合。 |
||||||||||||||
varyByQueryString |
可选的字符串属性。 指定字符串的分号分隔列表,这些字符串用于改变输出缓存。 默认情况下,这些字符串对应于使用 GET 方法属性发送的查询字符串值,或使用 POST 方法发送的参数。 当此属性设置为多个参数时,输出缓存包含所请求文档的不同版本,用于指定参数的各种组合。 可能的值包括 none、星号 (*)和任何有效的查询字符串或 POST 参数名称。 |
子元素
无。
配置示例
以下配置示例启用用户模式缓存和内核模式缓存,这两者在 IIS 7.0 中默认处于启用状态。 还使用 <profiles>
元素所包含的 <add>
元素为文件扩展名为 .asp 的文件启用输出缓存。 还使用 policy 属性输出页面,直到页面更改;使用 kernelCachePolicy 属性对内核缓存执行相同的操作。
<configuration>
<system.webServer>
<caching enabled="true" enableKernelCache="true">
<profiles>
<add extension=".asp" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
</profiles>
</caching>
</system.webServer>
</configuration>
下面的代码示例将最大输出缓存大小设置为 1 GB,并将可存储在输出缓存中的响应的最大大小设置为 512 KB。
<configuration>
<system.webServer>
<caching enabled="true" enableKernelCache="true" maxCacheSize="1000" maxResponseSize="512000"/>
</system.webServer>
</configuration>
代码示例
以下示例配置文件扩展名为 .asp 的文件的页面输出缓存,并将 IIS 配置为在用户模式和内核模式下缓存,直到 ASP 文件更改。
AppCmd.exe
appcmd.exe set config -section:system.webServer/caching /+"profiles.[extension='asp',policy='CacheUntilChange',kernelCachePolicy='CacheUntilChange']" /commit:apphost
注意
使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost
。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。
C#
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample {
private static void Main() {
using(ServerManager serverManager = new ServerManager()) {
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection cachingSection = config.GetSection("system.webServer/caching");
ConfigurationElementCollection profilesCollection = cachingSection.GetCollection("profiles");
ConfigurationElement addElement = profilesCollection.CreateElement("add");
addElement["extension"] = @"asp";
addElement["policy"] = @"CacheUntilChange";
addElement["kernelCachePolicy"] = @"CacheUntilChange";
profilesCollection.AddAt(0, addElement);
serverManager.CommitChanges();
}
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Module Sample
Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim cachingSection As ConfigurationSection = config.GetSection("system.webServer/caching")
Dim profilesCollection As ConfigurationElementCollection = cachingSection.GetCollection("profiles")
Dim addElement As ConfigurationElement = profilesCollection.CreateElement("add")
addElement("extension") = "asp"
addElement("policy") = "CacheUntilChange"
addElement("kernelCachePolicy") = "CacheUntilChange"
profilesCollection.AddAt(0, addElement)
serverManager.CommitChanges()
End Sub
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var cachingSection = adminManager.GetAdminSection("system.webServer/caching", "MACHINE/WEBROOT/APPHOST");
var profilesCollection = cachingSection.ChildElements.Item("profiles").Collection;
var addElement = profilesCollection.CreateNewElement("add");
addElement.Properties.Item("extension").Value = "asp";
addElement.Properties.Item("policy").Value = "CacheUntilChange";
addElement.Properties.Item("kernelCachePolicy").Value = "CacheUntilChange";
profilesCollection.AddElement(addElement, 0);
adminManager.CommitChanges();
VBScript
Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set cachingSection = adminManager.GetAdminSection("system.webServer/caching", "MACHINE/WEBROOT/APPHOST")
Set profilesCollection = cachingSection.ChildElements.Item("profiles").Collection
Set addElement = profilesCollection.CreateNewElement("add")
addElement.Properties.Item("extension").Value = "asp"
addElement.Properties.Item("policy").Value = "CacheUntilChange"
addElement.Properties.Item("kernelCachePolicy").Value = "CacheUntilChange"
profilesCollection.AddElement addElement, 0
adminManager.CommitChanges()