Client Cache <clientCache>

概要

<staticContent> 要素の <clientCache> 要素は、IIS 7 以降が Web クライアントに送信するキャッシュ関連の HTTP ヘッダーを指定します。これは、WEB クライアントとプロキシ サーバーが IIS 7 以降で返すコンテンツをキャッシュする方法を制御します。

たとえば、httpExpires 属性はコンテンツの有効期限が切れる日時を指定し、IIS 7 以降は HTTP の "Expires" ヘッダーを応答に追加します。 httpExpires 属性の値は、RFC 1123 の仕様に従って完全に書式設定された日付と時刻である必要があります。 次に例を示します。

2010 年 1 月 01 日 (金) 12:00:00 GMT

Note

httpExpires 属性を使用するには、cacheControlMode 属性の値を UseExpires に設定する必要があります。

Request For Comments (RFC) 2616 の HTTP 1.1 仕様では、HTTP "Cache-Control" ヘッダーに使用できるいくつかの値 ("no-cache"、"private"、"public," など) が指定されています。これらの各ヘッダー値により、Web クライアントとプロキシ サーバーは、コンテンツをキャッシュする必要があるかどうかを認識できます。

  • "no-cache" ヘッダーを含むコンテンツは、どのエンティティでもキャッシュされません。 - "private" ヘッダーを含むコンテンツは、プロキシ サーバーによってキャッシュされるべきではありませんが、Web クライアントによってキャッシュされる可能性があります。 - "public" ヘッダーを含むコンテンツは、任意のエンティティによってキャッシュできます。

    Note

    上記の一覧は包括的ではありません。"Cache-Control" ヘッダーで指定できる値は、これら以外に多数あります。

HTTP "Cache-Control" ヘッダーには、コンテンツの明示的な有効期限を指定する "max-age" などのディレクティブを追加できます。 IIS 7 では、"max-age" ディレクティブは cacheControlMaxAge 属性を使用して構成されます。 "Expires" と "max-age" の設定は多少似ていますが、"max-age" ディレクティブは "Expires" よりも優先されます。

Note

cacheControlMaxAge 属性を使用するには、cacheControlMode 属性の値を UseMaxAge に設定する必要があります。

HTTP キャッシュ ヘッダーと日付/時刻形式の詳細については、次の Request For Comments (RFC) の記事を参照してください。

  • RFC 1123 - インターネット ホストの要件 - アプリケーションとサポート
  • RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1"

互換性

バージョン メモ
IIS 10.0 <clientCache> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <clientCache> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 HTTP ETag ヘッダーを計算して設定するかどうかを指定する setEtag 属性が追加されました。
IIS 7.5 <clientCache> 要素は IIS 7.5 では変更されませんでした。
IIS 7.0 <staticContent> 要素の <clientCache> 要素は IIS 7.0 で導入されました。
IIS 6.0 <clientCache> 要素は、次の IIS 6.0 メタベース プロパティを置き換えます。
  • CacheControlCustom
  • CacheControlMaxAge
  • CacheControlNoCache
  • HttpExpires

段取り

<staticContent> 要素の <clientCache> 要素は、IIS 7 以降の既定のインストールに含まれています。

操作方法

Web サイトまたはアプリケーションのキャッシュの有効期限を構成する方法

  1. インターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] を選択し、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順に選択します。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] を選択します。
      • [管理ツール] を選択し、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンを選択し、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順に選択します。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順に選択します。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウで、キャッシュを無効にするサイト、アプリケーション、またはディレクトリに移動します。

  3. [ホーム] ウィンドウで、[HTTP 応答ヘッダー] をダブルクリックします。
    Screenshot that shows the Default Web Site Home pane, with H T T P Response Headers selected.

  4. [HTTP 応答ヘッダー] ページの [操作] ウィンドウで、[共通ヘッダーの設定] をクリックします。
    Screenshot that shows the H T T P Response Headers pane in the I I S Manager.

  5. [Set Common HTTP Response Headers]\(共通 HTTP 応答ヘッダーの設定\) ダイアログ ボックスで、Web コンテンツを期限切れにするチェック ボックスをオンにし、特定の間隔または特定の時刻に期限切れにするオプションを選択し、[OK] を選択します。
    Screenshot that shows the Set Common H T T P Response Headers dialog box. Enable H T T P keep alive is selected.
    Screenshot that shows the Set Common H T T P Response Headers dialog box. After is selected under the Expire Web content checkbox.

Web サイトまたはアプリケーションのキャッシュを無効にする方法

  1. インターネット インフォメーション サービス (IIS) マネージャーを開きます。

    • Windows Server 2012 または Windows Server 2012 R2 を使用している場合:

      • タスク バーで、[サーバー マネージャー] を選択し、[ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順に選択します。
    • Windows 8 または Windows 8.1 を使用している場合:

      • Windows キーを押しながら文字 X を押し、[コントロール パネル] を選択します。
      • [管理ツール] を選択し、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
    • Windows Server 2008 または Windows Server 2008 R2 を使用している場合:

      • タスク バーで、[スタート] ボタンを選択し、[管理ツール][インターネット インフォメーション サービス (IIS) マネージャー] の順に選択します。
    • Windows Vista または Windows 7 を使用している場合:

      • タスク バーで、[スタート][コントロール パネル] の順に選択します。
      • [管理ツール] をダブルクリックし、[インターネット インフォメーション サービス (IIS) マネージャー] をダブルクリックします。
  2. [接続] ウィンドウで、キャッシュを無効にするサイト、アプリケーション、またはディレクトリに移動します。

  3. [ホーム] ウィンドウで、[HTTP 応答ヘッダー] をダブルクリックします。
    Screenshot that shows the Default Web Site Home pane. H T T P Response Headers is selected.

  4. [HTTP 応答ヘッダー] ページの [操作] ウィンドウで、[共通ヘッダーの設定] をクリックします。
    Screenshot that shows the H T T P Response Headers pane.

  5. [Set Common HTTP Response Headers]\(共通 HTTP 応答ヘッダーの設定\) ダイアログ ボックスで、Web コンテンツを期限切れにするチェック ボックスをオンにし、[即時] を選択し、[OK] を選択します。
    Screenshot that shows the Set Common H T T P Response Headers dialog box. Immediately is selected under the Expire Web content checkbox.

構成

属性

属性 説明
cacheControlCustom 省略可能な文字列属性。

カスタム HTTP 1.1 キャッシュ制御ディレクティブを指定します。
cacheControlMaxAge 省略可能な timeSpan 属性。

キャッシュ制御値の最大有効期間 (秒単位) を指定します。

既定値は 1.00:00:00 (1 日) です。
cacheControlMode 省略可能な列挙型属性。

クライアント キャッシュに使用するモードを指定します。

cacheControlMode 属性には、次のいずれかの値を指定できます。

既定値は、NoControl です。
Value 説明
NoControl 応答に Cache-Control ヘッダーまたは Expires ヘッダーを追加しません。

この数値は 0 です。
DisableCache Cache-Control: no-cache ヘッダーを応答に追加します。

この数値は 1 です。
UseMaxAge Cache-Control: max-age=<nnn> ヘッダーを CacheControlMaxAge 属性に指定された値に基づく応答に追加します。

この数値は 2 です。
UseExpires httpExpires 属性で指定された日付に基づいて、有効期限が切れる <date> ヘッダーを応答に追加します。

この数値は 3 です。
httpExpires 省略可能な文字列属性。

クライアントにキャッシュされたページが古いと見なされる日時を指定します。 (日付と時刻は、Request for Comments 1123 の仕様に従って書式設定されます)。この値は、HTML ファイル ヘッダー内のブラウザーに返されます。 ユーザー エージェントは、指定された値を現在の日付と比較して、キャッシュされたページを表示するか、サーバーに更新されたページを要求するかを決定します。
setEtag 省略可能な Boolean 属性です。

HTTP ETag ヘッダーを計算して設定するかどうかを指定します。 ETag ヘッダーは Web キャッシュの検証に使用され、コンテンツに変更が加えられていなければ、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()