Share via


WebRequest.CachePolicy Properti

Definisi

Mendapatkan atau menetapkan kebijakan cache untuk permintaan ini.

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

Nilai Properti

Objek RequestCachePolicy yang menentukan kebijakan cache.

Contoh

Contoh kode berikut menunjukkan pengaturan kebijakan cache untuk permintaan 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;
}

Keterangan

Kebijakan cache saat ini dan keberadaan sumber daya yang diminta di cache menentukan apakah respons dapat diambil dari cache. Menggunakan respons cache biasanya meningkatkan performa aplikasi, tetapi ada risiko bahwa respons dalam cache tidak cocok dengan respons di server.

Kebijakan cache default dapat ditentukan dalam file konfigurasi Machine.config atau dengan mengatur DefaultCachePolicy properti untuk permintaan yang menggunakan skema URI Hypertext Transfer Protocol (HTTP) atau Secure Hypertext Transfer Protocol (HTTPS).

Salinan sumber daya hanya ditambahkan ke cache jika aliran respons untuk sumber daya diambil dan dibaca ke akhir aliran. Jadi permintaan lain untuk sumber daya yang sama dapat menggunakan salinan yang di-cache, tergantung pada tingkat kebijakan cache untuk permintaan ini.

Berlaku untuk

Lihat juga