用戶端快取 < 用戶端Cache>

概觀

元素 <clientCache> 的 元素會指定 IIS 7 和更新版本傳送至 Web 用戶端的 <staticContent> 快取相關 HTTP 標頭,以控制 Web 用戶端和 Proxy 伺服器如何快取 IIS 7 和更新版本傳回的內容。

例如, HTTPExpires 屬性會指定內容的到期日期和時間,而 IIS 7 和更新版本會將 HTTP 「Expires」 標頭新增至回應。 HTTPExpires屬性的值必須是 RFC 1123 中規格之後的完整格式日期和時間。 例如:

2010 年 1 月 1 日 12:00:00 GMT

注意

若要使用 HTTPExpires 屬性,您必須將 cacheControlMode 屬性的值設定為 UseExpires

要求批註中的 HTTP 1.1 規格 (RFC) 2616 會指定 HTTP 「Cache-Control」 標頭的數個可能值,例如 「no-cache」、「private」、「public」 等等。這些標頭值都讓 Web 用戶端和 Proxy 伺服器知道是否應該快取內容:

  • 包含「無快取」標頭的內容不應由任何實體快取。 - 包含「私人」標頭的內容不應由 Proxy 伺服器快取,但可由 Web 用戶端快取。 - 包含「公用」標頭的內容可由任何實體快取。

    注意

    上述清單並不完整 - 有許多額外的值可由 「Cache-Control」 標頭指定。

您可以將其他指示詞新增至 HTTP 「Cache-Control」 標頭,例如 「max-age」,以指定內容的明確到期時間。 在 IIS 7 中,「max-age」 指示詞是透過 cacheControlMaxAge 屬性來設定。 雖然 「Expires」 和 「max-age」 設定有點類似,但 「max-age」 指示詞優先于 「Expires」。

注意

若要使用 cacheControlMaxAge 屬性,您必須將 cacheControlMode 屬性的值設定為 UseMaxAge

如需 HTTP 快取標頭和日期/時間格式的詳細資訊,請參閱下列要求批註 (RFC) 文章:

  • RFC 1123 - 「網際網路主機的需求 -- 應用程式和支援」
  • RFC 2616 - 「超文字傳輸通訊協定 -- HTTP/1.1」

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <clientCache>
IIS 8.5 未在 IIS 8.5 中修改專案 <clientCache>
IIS 8.0 setEtag已新增 屬性,以指定是否計算並設定 HTTP ETag 標頭。
IIS 7.5 未在 IIS 7.5 中修改專案 <clientCache>
IIS 7.0 元素 <clientCache><staticContent> 元素是在 IIS 7.0 中引進。
IIS 6.0 元素 <clientCache> 會取代下列 IIS 6.0 中繼基底屬性:
  • CacheControlCustom
  • CacheControlMaxAge
  • CacheControlNoCache
  • HttpExpires

安裝程式

元素 <clientCache><staticContent> 元素包含在 IIS 7 和更新版本的預設安裝中。

作法

如何設定網站或應用程式的快取到期日

  1. 開啟 [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
  2. 在 [ 連線] 窗格中,移至您要停用快取的月臺、應用程式或目錄。

  3. 在 [ 首頁] 窗格中,按兩下 [HTTP 回應標頭]。
    顯示 [預設網站首頁] 窗格的螢幕擷取畫面,其中已選取 [H T P 回應標頭]。

  4. 在 [HTTP 回應標頭]窗格中,按一下 [動作] 窗格中的[設定一般標頭...]。
    顯示 [I I S 管理員] 中 [H T T P 回應標頭] 窗格的螢幕擷取畫面。

  5. 在 [ 設定一般 HTTP 回應標頭 ] 對話方塊中,核取要過期 Web 內容的方塊,選取在特定間隔或特定時間到期的選項,然後按一下 [ 確定]。
    顯示 [設定一般 H T T P 回應標頭] 對話方塊的螢幕擷取畫面。已選取 [啟用 H T T P 保持運作]。
    顯示 [設定一般 H T T P 回應標頭] 對話方塊的螢幕擷取畫面。在 [到期 Web 內容] 核取方塊下選取 [ 之後]。

如何停用網站或應用程式的快取

  1. 開啟 [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
  2. 在 [ 連線] 窗格中,移至您要停用快取的月臺、應用程式或目錄。

  3. 在 [ 首頁] 窗格中,按兩下 [HTTP 回應標頭]。
    顯示 [預設網站首頁] 窗格的螢幕擷取畫面。已選取 H T T P 回應標頭。

  4. 在 [HTTP 回應標頭]窗格中,按一下 [動作] 窗格中的[設定一般標頭...]。
    顯示 [H T T P 回應標頭] 窗格的螢幕擷取畫面。

  5. 在 [ 設定一般 HTTP 回應標頭 ] 對話方塊中,核取要過期 Web 內容的方塊,選取 [ 立即],然後按一下 [ 確定]。
    顯示 [設定一般 H T T P 回應標頭] 對話方塊的螢幕擷取畫面。緊接在 [過期 Web 內容] 核取方塊底下選取。

組態

屬性

屬性 描述
cacheControlCustom 選擇性字串屬性。

指定自訂 HTTP 1.1 快取控制指示詞。
cacheControlMaxAge 選擇性的 timeSpan 屬性。

指定快取控制項值) 以秒為單位的最大存留期 (。

預設值為 1.00:00:00 (1 天) 。
cacheControlMode 選擇性列舉屬性。

指定要用於用戶端快取的模式。

cacheControlMode屬性可以是下列其中一個可能的值。

預設值為 NoControl
描述
NoControl 不會將Cache-Control或 Expires 標頭新增至回應。

數值為 0
DisableCache 將 Cache-Control:no-cache 標頭新增至回應。

數值為 1
UseMaxAge 根據CacheControlMaxAge屬性中指定的值,將 Cache-Control: max-age= < nnn > 標頭新增至回應。

數值為 2
UseExpires 根據HTTPExpires屬性中指定的日期,將 Expires: < date > 標頭新增至回應。

數值為 3
httpExpires 選擇性字串屬性。

指定在用戶端上快取的頁面被視為過時的日期和時間。 (日期和時間會根據 要求批註 1123中的規格來格式化。) 此值會傳回 HTML 檔案標頭中的瀏覽器。 使用者代理程式會比較指定的值與目前的日期,以判斷是要顯示快取的頁面,還是從伺服器要求更新的頁面。
setEtag 選擇性的 Boolean 屬性。

指定是否計算並設定 HTTP ETag 標頭。 ETag 標頭用於 Web 快取驗證,如果尚未對內容進行任何變更,則網頁伺服器就不需要傳送完整回應。 setEtag 可以在路徑 system.webServer/staticContent 的組態編輯器中設定。

預設值是 true

子元素

無。

組態範例

下列組態範例會將 HTTP 「Cache-Control: no-cache」 標頭新增至回應,藉此停用要求的快取。

<configuration>
   <system.webServer>
      <staticContent>
         <clientCache cacheControlMode="DisableCache" />
      </staticContent>
   </system.webServer>
</configuration>

下列組態範例會將 HTTP 「Expires: Tue, 19 Jan 2038 03:14:07 GMT」 標頭新增至回應,以設定要求從現在起到期數年。

<configuration>
   <system.webServer>
      <staticContent>
         <clientCache cacheControlMode="UseExpires"
            httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
      </staticContent>
   </system.webServer>
</configuration>

範例程式碼

下列程式碼範例會將 HTTP 「Cache-Control: no-cache」 標頭新增至回應,藉此停用要求的快取。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/staticContent /clientCache.cacheControlMode:"DisableCache"

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.GetWebConfiguration("Default Web Site");
         ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent");

         ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
         clientCacheElement["cacheControlMode"] = @"DisableCache";

         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.GetWebConfiguration("Default Web Site")
      Dim staticContentSection As ConfigurationSection = config.GetSection("system.webServer/staticContent")

      Dim clientCacheElement As ConfigurationElement = staticContentSection.GetChildElement("clientCache")
      clientCacheElement("cacheControlMode") = "DisableCache"

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var staticContentSection = adminManager.GetAdminSection("system.webServer/staticContent", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var clientCacheElement = staticContentSection.ChildElements.Item("clientCache");
clientCacheElement.Properties.Item("cacheControlMode").Value = "DisableCache";

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set staticContentSection = adminManager.GetAdminSection("system.webServer/staticContent", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set clientCacheElement = staticContentSection.ChildElements.Item("clientCache")
clientCacheElement.Properties.Item("cacheControlMode").Value = "DisableCache"

adminManager.CommitChanges()

下列程式碼範例會將 HTTP 「Expires: Tue, 19 Jan 2038 03:14:07 GMT」 標頭新增至回應,以設定要求從現在起到期數年。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/staticContent /clientCache.cacheControlMode:"UseExpires"

appcmd.exe set config "Default Web Site" -section:system.webServer/staticContent /clientCache.httpExpires:"Tue, 19 Jan 2038 03:14:07 GMT"

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.GetWebConfiguration("Default Web Site");
         ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent");

         ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
         clientCacheElement["cacheControlMode"] = @"UseExpires";
         clientCacheElement["httpExpires"] = @"Tue, 19 Jan 2038 03:14:07 GMT";

         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.GetWebConfiguration("Default Web Site")
      Dim staticContentSection As ConfigurationSection = config.GetSection("system.webServer/staticContent")

      Dim clientCacheElement As ConfigurationElement = staticContentSection.GetChildElement("clientCache")
      clientCacheElement("cacheControlMode") = "UseExpires"
      clientCacheElement("httpExpires") = "Tue, 19 Jan 2038 03:14:07 GMT"

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var staticContentSection = adminManager.GetAdminSection("system.webServer/staticContent", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var clientCacheElement = staticContentSection.ChildElements.Item("clientCache");
clientCacheElement.Properties.Item("cacheControlMode").Value = "UseExpires";
clientCacheElement.Properties.Item("httpExpires").Value = "Tue, 19 Jan 2038 03:14:07 GMT";

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set staticContentSection = adminManager.GetAdminSection("system.webServer/staticContent", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set clientCacheElement = staticContentSection.ChildElements.Item("clientCache")
clientCacheElement.Properties.Item("cacheControlMode").Value = "UseExpires"
clientCacheElement.Properties.Item("httpExpires").Value = "Tue, 19 Jan 2038 03:14:07 GMT"

adminManager.CommitChanges()