WebRequest.CachePolicy プロパティ

定義

この要求のキャッシュ ポリシーを取得または設定します。

public:
 virtual property System::Net::Cache::RequestCachePolicy ^ CachePolicy { System::Net::Cache::RequestCachePolicy ^ get(); void set(System::Net::Cache::RequestCachePolicy ^ value); };
public virtual System.Net.Cache.RequestCachePolicy? CachePolicy { get; set; }
public virtual System.Net.Cache.RequestCachePolicy CachePolicy { get; set; }
member this.CachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Overridable Property CachePolicy As RequestCachePolicy

プロパティ値

キャッシュ ポリシーを定義する RequestCachePolicy オブジェクト。

次のコード例は、Web 要求のキャッシュ ポリシーを設定する方法を示しています。

// The following method demonstrates overriding the
// caching policy for a request.
static WebResponse^ GetResponseNoCache( Uri^ uri )
{
   // Set a default policy level for the "http:" and "https" schemes.
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::Default );
   HttpWebRequest::DefaultCachePolicy = policy;

   // Create the request.
   WebRequest^ request = WebRequest::Create( uri );

   // Define a cache policy for this request only. 
   HttpRequestCachePolicy^ noCachePolicy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::NoCacheNoStore );
   request->CachePolicy = noCachePolicy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"IsFromCache? {0}", response->IsFromCache );
   
   return response;
}
// The following method demonstrates overriding the
// caching policy for a request.
public static WebResponse GetResponseNoCache(Uri uri)
{
    // Set a default policy level for the "http:" and "https" schemes.
    HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
    HttpWebRequest.DefaultCachePolicy = policy;
    // Create the request.
    WebRequest request = WebRequest.Create(uri);
    // Define a cache policy for this request only.
    HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
    request.CachePolicy = noCachePolicy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("IsFromCache? {0}", response.IsFromCache);
    return response;
}

注釈

現在のキャッシュ ポリシーと、要求されたリソースがキャッシュに存在することで、キャッシュから応答を取得できるかどうかが決まります。 キャッシュされた応答を使用すると、通常、アプリケーションのパフォーマンスが向上しますが、キャッシュ内の応答がサーバー上の応答と一致しないリスクがあります。

既定のキャッシュ ポリシーは、Machine.config構成ファイルで指定するか、ハイパーテキスト転送プロトコル (HTTP) または Secure Hypertext Transfer Protocol (HTTPS) URI スキームを使用する要求の プロパティを設定 DefaultCachePolicy することで指定できます。

リソースのコピーは、リソースの応答ストリームが取得され、ストリームの末尾に読み取られた場合にのみキャッシュに追加されます。 そのため、同じリソースに対する別の要求では、この要求のキャッシュ ポリシー レベルに応じて、キャッシュされたコピーを使用できます。

適用対象

こちらもご覧ください