WebRequest.CachePolicy WebRequest.CachePolicy WebRequest.CachePolicy WebRequest.CachePolicy Property

定义

获取或设置此请求的缓存策略。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 配置文件中指定,也可以通过为使用超DefaultCachePolicy文本传输协议(HTTP)或安全超文本传输协议(HTTPS) URI 方案的请求设置属性来指定。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.

适用于

另请参阅