新增快取設定檔 <>
概觀
專案的 <add>
元素 <profiles>
會將輸出快取設定檔新增至輸出快取設定檔的集合。
相容性
版本 | 備註 |
---|---|
IIS 10.0 | 未在 IIS 10.0 中修改專案 <add> 。 |
IIS 8.5 | 未在 IIS 8.5 中修改專案 <add> 。 |
IIS 8.0 | 未在 IIS 8.0 中修改專案 <add> 。 |
IIS 7.5 | 未在 IIS 7.5 中修改專案 <add> 。 |
IIS 7.0 | 元素 <add> 的 <profiles> 元素是在 IIS 7.0 中引進。 |
IIS 6.0 | N/A |
安裝程式
元素 <add>
的 <profiles>
元素包含在 IIS 7 的預設安裝中。
作法
如何設定頁面輸出快取
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8 或 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
如果您使用 Windows Server 2008 或 Windows Server 2008 R2:
- 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
如果您使用 Windows Vista 或 Windows 7:
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager。
在 [ 連線] 窗格中,移至您要設定頁面輸出快取的連線、月臺、應用程式或目錄。
在 [ 動作 ] 窗格中,按一下 [ 新增...]
在 [ 新增快取規則 ] 對話方塊中,于 [副檔名 ] 方塊 中輸入您想要快取的副檔名,然後選取 [ 使用者模式 快取] 選項、[ 核心模式快取 ] 選項或兩者。
組態
屬性
屬性 | 描述 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
duration |
選擇性的 timeSpan 屬性。 指定快取頁面或使用者控制項) 以秒為單位的時間 ( 預設值為 00:00:30 。 |
||||||||||||||
extension |
必要的字串屬性。 指定您要快取之檔案的副檔名。 |
||||||||||||||
kernelCachePolicy |
選擇性列舉屬性。 設定核心快取原則。 kernelCachePolicy屬性可以是下列其中一個值。 預設值為 DontCache 。
|
||||||||||||||
location |
選擇性列舉屬性。 指定有效值,以控制資源輸出快取 HTTP 回應的位置。 location屬性可以是下列其中一個可能的值。 預設值為 Server 。
|
||||||||||||||
policy |
選擇性列舉屬性。 設定輸出快取原則。 原則屬性可以是下列其中一個可能的值。 預設值為 DontCache 。
|
||||||||||||||
varyByHeaders |
選擇性字串屬性。 指定用來變更輸出快取的 HTTP 標頭分號分隔清單。 當此屬性設定為多個標頭時,輸出快取會針對指定標頭的每個組合,包含不同版本的要求檔。 |
||||||||||||||
varyByQueryString |
選擇性字串屬性。 指定用來變更輸出快取的以分號分隔的字串清單。 根據預設,這些字串會對應至使用 GET 方法屬性傳送的查詢字串值,或使用 POST 方法傳送的參數。 當此屬性設定為多個參數時,輸出快取會針對每個指定參數的組合,包含不同版本的要求檔。 可能的值為 none、星號 (*) ,以及任何有效的查詢字串或 POST 參數名稱。 |
子元素
無。
組態範例
下列組態範例會啟用使用者模式快取和核心模式快取,這兩者在 IIS 7.0 中預設都會啟用。 它也會使用 <add>
專案所包含的 <profiles>
專案,為副檔名為 .asp 的檔案啟用輸出快取。 它也會使用 原則 屬性來輸出快取頁面,直到頁面變更為止;它會使用 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 來設定這些設定時,請務必將 認可 參數 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()