WebRequest.CachePolicy 屬性

定義

取得或設定這個要求的快取原則。Gets or sets the cache policy for this request.

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; }
member this.CachePolicy : System.Net.Cache.RequestCachePolicy with get, set
Public Overridable Property CachePolicy As RequestCachePolicy

屬性值

定義快取原則的 RequestCachePolicy 物件。A RequestCachePolicy object that defines a cache policy.

範例

下列程式碼範例將示範如何設定 Web 要求的快取原則。The following code example demonstrates setting the cache policy for a Web request.

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

備註

目前的快取原則和快取中所要求資源的存在,會決定是否可從快取中抓取回應。The current cache policy and the presence of the requested resource in the cache determine whether a response can be retrieved from the cache. 使用快取的回應通常可改善應用程式效能,但快取中的回應與伺服器上的回應並不相符。Using cached responses usually improves application performance, but there is a risk that the response in the cache does not match the response on the server.

您可以在 machine.config 設定檔中指定預設快取原則,或為使用超文字傳輸通訊協定(HTTP)或安全超文字傳輸通訊協定(HTTPS) URI 配置的要求設定 DefaultCachePolicy 屬性。Default cache policy can be specified in the Machine.config configuration file or by setting the DefaultCachePolicy property for requests that use the Hypertext Transfer Protocol (HTTP) or Secure Hypertext Transfer Protocol (HTTPS) URI scheme.

只有在抓取資源的回應資料流程並讀取至資料流程結尾時,才會將資源複本新增至快取。A copy of a resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. 因此,相同資源的另一個要求可以使用快取的複本,視此要求的快取原則層級而定。So another request for the same resource could use a cached copy, depending on the cache policy level for this request.

適用於

另請參閱