RequestCacheLevel 枚举

定义

为使用 WebRequest 及其派生类获取的资源指定缓存行为。Specifies caching behavior for resources obtained using WebRequest and its derived classes.

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

字段

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. 这是 .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.

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.

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

如果时间戳与服务器上的资源的时间戳相同,则使用资源的缓存副本满足请求;否则从服务器下载资源,将资源展示给调用方,然后再存储在缓存中。Satisfies a request by using the cached copy of the resource if the timestamp is the same as the timestamp of the resource on the server; otherwise, the resource is downloaded from the server, presented to the caller, and stored in the cache.

示例

下面的代码示例创建一个策略, 该策略仅在缓存中返回资源。The following code example creates policy that returns a resource only if it is in the cache.

static WebResponse^ GetResponseFromCache( Uri^ uri )
{
   RequestCachePolicy^ policy = gcnew RequestCachePolicy( RequestCacheLevel::CacheOnly );
   WebRequest^ request = WebRequest::Create( uri );
   request->CachePolicy = policy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"Policy level is {0}.", policy->Level );
   Console::WriteLine( L"Is the response from the cache? {0}", response->IsFromCache );
   return response;
}
public static WebResponse GetResponseFromCache(Uri uri)
{
     RequestCachePolicy policy = 
        new  RequestCachePolicy( RequestCacheLevel.CacheOnly);
    WebRequest request = WebRequest.Create(uri);
    request.CachePolicy = policy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("Policy level is {0}.", policy.Level.ToString());
    Console.WriteLine("Is the response from the cache? {0}", response.IsFromCache);  
    return response;
    
} 

注解

此枚举的成员用于初始化RequestCachePolicy对象。Members of this enumeration are used to initialize RequestCachePolicy objects. RequestCachePolicy对象的当前设置HttpRequestCachePolicy.Level在属性中可用。The current setting for a RequestCachePolicy object is available in the HttpRequestCachePolicy.Level property.

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

如果缓存行为为CacheIfAvailableRevalidate, 则在检索到资源的响应流并将其读取到流的末尾时, 仅将请求的资源的副本添加到缓存中。If the cache behavior is CacheIfAvailable or Revalidate, a copy of a requested resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. 对于CacheIfAvailable, 对同一资源的后续请求将使用缓存的副本。With CacheIfAvailable, subsequent requests for the same resource would use a cached copy. 对于Revalidate, 如果缓存资源的时间戳与服务器上资源的时间戳相同, 则对相同资源的后续请求将使用缓存的副本。With Revalidate, subsequent requests for the same resource would use a cached copy if the timestamp for the cached resource is the same as the timestamp of the resource on the server.

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

适用于

另请参阅