クライアント キャッシュ <clientCache>
- 概要
- 互換性
- セットアップ
- 方法
- 構成
- サンプル コード
※本ページに挿入されている画像をクリックすると、画像全体が別ウィンドウで表示されます。
概要
<staticContent> 要素の <clientCache>
要素は、インターネット インフォメーション サービス (IIS) 7.0 が Web クライアントに送信するキャッシュ関連の HTTP ヘッダーを指定します。これは、IIS 7.0 から返されるコンテンツを Web クライアントとプロキシ サーバーでキャッシュする方法を制御します。
たとえば、httpExpires 属性は、コンテンツの期限が切れて応答に HTTP "Expires" ヘッダーが追加される日時を指定します。httpExpires 属性の値は、RFC (Request for Comments) 1123 の規格に従った完全な形式の日時である必要があります。次に例を示します。
Fri, 01 Jan 2010 12:00:00 GMT
注 : httpExpires 属性を使用するには、cacheControlMode 属性の値を UseExpires に設定する必要があります。
RFC 2616 の HTTP 1.1 規格では、"no-cache"、"private"、"public" など、HTTP "Cache-Control" ヘッダーに使用できる複数の値が指定されています。これらの各ヘッダー値により、Web クライアントとプロキシ サーバーはコンテンツをキャッシュすべきかどうかを把握します。
"no-cache" ヘッダーを持つコンテンツは、どのエンティティもキャッシュしません。
"private" ヘッダーを持つコンテンツは、プロキシ サーバーはキャッシュしませんが、Web クライアントはキャッシュできます。
"public" ヘッダーを持つコンテンツは、どのエンティティもキャッシュできます。
注 : 上記の一覧がすべてではありません。他にも "Cache-Control" ヘッダーで指定できる値が多数あります。
コンテンツの有効期限を明示的に指定する "max-age" などのディレクティブも HTTP "Cache-Control" ヘッダーに追加できます。IIS 7.0 では、"max-age" ディレクティブは cacheControlMaxAge 属性を使用して構成します。"Expires" 設定と "max-age" 設定は似ていますが、"max-age" ディレクティブは "Expires" に優先します。
注 : cacheControlMaxAge 属性を使用するには、cacheControlMode 属性の値を UseMaxAge に設定する必要があります。
HTTP キャッシュ ヘッダーおよび日時形式の詳細については、次の RFC の記事を参照してください。
- RFC 1123 - "Requirements for Internet Hosts -- Application and Support" (英語)
- RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1" (英語)
互換性
IIS 7.0 | IIS 6.0 | |
---|---|---|
注意 | <staticContent> の <clientCache> は IIS 7.0 で新たに導入された要素です。 |
<clientCache> 要素は、IIS 6.0 の次のメタベース プロパティに代わるものです。CacheControlCustom CacheControlMaxAge CacheControlNoCache HttpExpires |
セットアップ
<staticContent>
要素の <clientCache>
要素は、IIS 7.0 の既定のインストールに含まれています。
方法
Web サイトまたはアプリケーションのキャッシュの有効期限の構成方法
タスク バーで [スタート] をクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで、キャッシュを無効にするサイト、アプリケーション、またはディレクトリに移動します。
[ホーム] ウィンドウで [HTTP 応答ヘッダー] をダブルクリックします。
[HTTP 応答ヘッダー] ページで、[操作] ウィンドウの [共通ヘッダーの設定] をクリックします。
[共通 HTTP 応答ヘッダーの設定] ダイアログ ボックスで、Web コンテンツを期限切れにするボックスをオンにし、特定間隔後または特定時刻に期限切れにするオプションを選択して、[OK] をクリックします。
Web サイトまたはアプリケーションのキャッシュを無効にする方法
タスク バーで [スタート] をクリックし、[管理ツール] をポイントして [インターネット インフォメーション サービス (IIS) マネージャー] をクリックします。
[接続] ウィンドウで、キャッシュを無効にするサイト、アプリケーション、またはディレクトリに移動します。
[ホーム] ウィンドウで [HTTP 応答ヘッダー] をダブルクリックします。
[HTTP 応答ヘッダー] ページで、[操作] ウィンドウの [共通ヘッダーの設定] をクリックします。
[共通 HTTP 応答ヘッダーの設定] ダイアログ ボックスで、Web コンテンツを期限切れにするボックスをオンにし、[即時] を選択して [OK] をクリックします。
構成
属性
属性 | 説明 |
---|---|
cacheControlCustom |
オプションの string 属性。 カスタム HTTP 1.1 キャッシュ制御ディレクティブを指定します。 |
cacheControlMaxAge |
オプションの timeSpan 属性。 キャッシュ制御値の最長有効期間 (秒) を指定します。 既定値は 1.00:00:00 (1 日) です。 |
cacheControlMode |
オプションの enum 属性。 クライアントのキャッシュに使用するモードを指定します。 cacheControlMode 属性には、次のいずれかの値を指定できます。 既定値は NoControl です。-------------------------------------------------------- 値: NoControl 説明:
値: DisableCache 説明:
値: UseMaxAge 説明:
値: UseExpires 説明:
|
httpExpires |
オプションの string 属性。 クライアントでキャッシュされたページが期限切れと見なされる日時を指定します。(日時は RFC 1123 に従って書式設定します。)値は HTML ファイル ヘッダーでブラウザーに返されます。ユーザー エージェントは所与の値を現在の日付と比較して、キャッシュされたページを表示するかサーバーから更新されたページを要求するかを決定します。 |
子要素
なし。
構成サンプル
次の構成サンプルでは、応答に 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()