HttpRequestCacheLevel 枚举

定义

为使用超文本传输协议 (HTTP) 获取的资源指定缓存行为。Specifies caching behavior for resources obtained using the Hypertext Transfer protocol (HTTP).

public enum class HttpRequestCacheLevel
public enum HttpRequestCacheLevel
type HttpRequestCacheLevel = 
Public Enum HttpRequestCacheLevel
继承
HttpRequestCacheLevel

字段

BypassCache 1

使用服务器满足请求。Satisfies a request by using the server. 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。No entries are taken from caches, added to caches, or removed from caches between the client and server. 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。No entries are taken from caches, added to caches, or removed from caches between the client and server. 这是 .NET Framework 附带的计算机配置文件中指定的默认缓存行为。This is the default cache behavior specified in the machine configuration file that ships with the .NET Framework.

CacheIfAvailable 3

如果资源可用,则从缓存满足资源请求;否则,将资源请求发送到服务器。Satisfies a request for a resource from the cache if the resource is available; otherwise, sends a request for a resource to the server. 如果请求的项在客户端和服务器之间的任何缓存中可用,则可由中间缓存满足请求。If the requested item is available in any cache between the client and the server, the request might be satisfied by the intermediate cache.

CacheOnly 2

使用本地缓存资源满足请求;不发送对不在缓存中的项的请求。Satisfies a request using the locally cached resource; does not send a request for an item that is not in the cache. 当指定此缓存策略级别时,如果项不在客户端缓存中,则引发 WebException 异常。When this cache policy level is specified, a WebException exception is thrown if the item is not in the client cache.

CacheOrNextCacheOnly 7

从本地计算机的缓存或局域网上的远程缓存满足资源请求。Satisfies a request for a resource either from the local computer's cache or a remote cache on the local area network. 如果无法满足请求,则引发 WebException 异常。If the request cannot be satisfied, a WebException exception is thrown. 在 HTTP 缓存协议中,这是通过 only-if-cached 缓存控制指令实现的。In the HTTP caching protocol, this is achieved using the only-if-cached cache control directive.

Default 0

通过使用资源的缓存副本或通过将资源请求发送到服务器来满足资源请求。Satisfies a request for a resource either by using the cached copy of the resource or by sending a request for the resource to the server. 采取的操作取决于当前的缓存策略和缓存内容的生存期。The action taken is determined by the current cache policy and the age of the content in the cache. 这是大多数应用程序应使用的缓存级别。This is the cache level that should be used by most applications.

NoCacheNoStore 6

从不通过使用缓存中的资源来满足请求,也不缓存资源。Never satisfies a request by using resources from the cache and does not cache resources. 如果资源在本地缓存中,则会移除该资源。If the resource is present in the local cache, it is removed. 此策略级别指示中间缓存应移除资源。This policy level indicates to intermediate caches that they should remove the resource. 在 HTTP 缓存协议中,这是通过 no-cache 缓存控制指令实现的。In the HTTP caching protocol, this is achieved using the no-cache cache control directive.

Refresh 8

通过使用服务器或本地缓存以外的缓存来满足请求。Satisfies a request by using the server or a cache other than the local cache. 在请求可由中间缓存满足之前,该缓存必须通过服务器重新验证其缓存条目。Before the request can be satisfied by an intermediate cache, that cache must revalidate its cached entry with the server. 在 HTTP 缓存协议中,这是通过 max-age = 0 缓存控制指令和 no-cache Pragma 标头实现的。In the HTTP caching protocol, this is achieved using the max-age = 0 cache control directive and the no-cache Pragma header.

Reload 5

使用服务器满足请求。Satisfies a request by using the server. 响应可能保存在缓存中。The response might be saved in the cache. 在 HTTP 缓存协议中,这是通过 no-cache 缓存控制指令和 no-cache Pragma 标头实现的。In the HTTP caching protocol, this is achieved using the no-cache cache control directive and the no-cache Pragma header.

Revalidate 4

将缓存中的资源副本与服务器上的副本进行比较。Compares the copy of the resource in the cache with the copy on the server. 如果服务器上的副本较新,则用它来满足请求并替换缓存中的副本。If the copy on the server is newer, it is used to satisfy the request and replaces the copy in the cache. 如果缓存中的副本与服务器副本相同,则使用缓存副本。If the copy in the cache is the same as the server copy, the cached copy is used. 在 HTTP 缓存协议中,这是通过条件请求来实现的。In the HTTP caching protocol, this is achieved using a conditional request.

示例

下面的代码示例将应用程序域的缓存策略设置为默认值。The following code example sets the application domain's caching policy to Default.

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

注解

此枚举用于设置 HttpRequestCachePolicy 对象指定的缓存级别。This enumeration is used to set the cache level specified by HttpRequestCachePolicy objects.

BypassCache 值是在 .NET Framework 附带的计算机配置文件中指定的默认缓存行为。This BypassCache value is the default cache behavior specified in the machine configuration file that ships with the .NET Framework. 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。No entries are taken from caches, added to caches, or removed from caches between the client and server.

HttpWebRequest.DefaultCachePolicy 属性用于获取或设置 HttpWebRequest 实例的默认缓存策略。The HttpWebRequest.DefaultCachePolicy property is used to get or set the default cache policy for HttpWebRequest instances. WebRequest.DefaultCachePolicy 属性用于获取或设置 WebRequest 实例的默认缓存策略。The WebRequest.DefaultCachePolicy property is used to get or set the default cache policy for a WebRequest instance. CachePolicy 属性用于获取或设置特定请求的缓存策略。The CachePolicy property is used to get or set the cache policy for a specific request.

如果资源的响应流已检索并读取到流的末尾,则仅将资源的副本添加到缓存中。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 default cache policy level for this request.

适用于

另请参阅