WebRequest.CachePolicy Özellik

Tanım

Bu istek için önbellek ilkesini alır veya ayarlar.

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

Özellik Değeri

RequestCachePolicy

RequestCachePolicy Önbellek ilkesini tanımlayan nesne.

Örnekler

Aşağıdaki kod örneği, bir Web isteği için önbellek ilkesini ayarlamayı gösterir.

// 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;
}

Açıklamalar

Geçerli önbellek ilkesi ve istenen kaynağın önbellekte bulunması, önbellekten bir yanıt alınıp alınamayacağını belirler. Önbelleğe alınmış yanıtların kullanılması genellikle uygulama performansını artırır, ancak önbellekteki yanıtın sunucudaki yanıtla eşleşmeme riski vardır.

Varsayılan önbellek ilkesi, Machine.config yapılandırma dosyasında veya Köprü Metni Aktarım Protokolü (HTTP) veya Güvenli Köprü Metni Aktarım Protokolü (HTTPS) URI şemasını kullanan istekler için özelliği ayarlanarak DefaultCachePolicy belirtilebilir.

Bir kaynağın kopyası önbelleğe yalnızca kaynağın yanıt akışı alınır ve akışın sonuna okunursa eklenir. Bu nedenle aynı kaynağa yönelik başka bir istek, bu isteğin önbellek ilkesi düzeyine bağlı olarak önbelleğe alınmış bir kopya kullanabilir.

Şunlara uygulanır

Ayrıca bkz.