HttpRequestCacheLevel 枚举

定义

为使用超文本传输协议 (HTTP) 获取的资源指定缓存行为。

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

字段

BypassCache 1

使用服务器满足请求。 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。 这是 .NET Framework 附带的计算机配置文件中指定的默认缓存行为。

CacheIfAvailable 3

如果资源可用,则从缓存满足资源请求;否则,将资源请求发送到服务器。 如果请求的项在客户端和服务器之间的任何缓存中可用,则可由中间缓存满足请求。

CacheOnly 2

使用本地缓存资源满足请求;不发送对不在缓存中的项的请求。 当指定此缓存策略级别时,如果项不在客户端缓存中,则引发 WebException 异常。

CacheOrNextCacheOnly 7

从本地计算机的缓存或局域网上的远程缓存满足资源请求。 如果无法满足请求,则引发 WebException 异常。 在 HTTP 缓存协议中,这是通过 only-if-cached 缓存控制指令实现的。

Default 0

通过使用资源的缓存副本或通过将资源请求发送到服务器来满足资源请求。 采取的操作取决于当前的缓存策略和缓存内容的生存期。 这是大多数应用程序应使用的缓存级别。

NoCacheNoStore 6

从不通过使用缓存中的资源来满足请求,也不缓存资源。 如果资源在本地缓存中,则会移除该资源。 此策略级别指示中间缓存应移除资源。 在 HTTP 缓存协议中,这是通过 no-cache 缓存控制指令实现的。

Refresh 8

通过使用服务器或本地缓存以外的缓存来满足请求。 在请求可由中间缓存满足之前,该缓存必须通过服务器重新验证其缓存条目。 在 HTTP 缓存协议中,这是通过 max-age = 0 缓存控制指令和 no-cache Pragma 标头实现的。

Reload 5

使用服务器满足请求。 响应可能保存在缓存中。 在 HTTP 缓存协议中,这是通过 no-cache 缓存控制指令和 no-cache Pragma 标头实现的。

Revalidate 4

将缓存中的资源副本与服务器上的副本进行比较。 如果服务器上的副本较新,则用它来满足请求并替换缓存中的副本。 如果缓存中的副本与服务器副本相同,则使用缓存副本。 在 HTTP 缓存协议中,这是通过条件请求来实现的。

示例

下面的代码示例将应用程序域的缓存策略设置为“默认”。

// 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 缓存级别。

BypassCache此值是.NET Framework随附的计算机配置文件中指定的默认缓存行为。 在客户端和服务器之间没有项从缓存中取出、添加到缓存中或从缓存中移除。

HttpWebRequest.DefaultCachePolicy 属性用于获取或设置实例的默认缓存策略 HttpWebRequest 。 该 WebRequest.DefaultCachePolicy 属性用于获取或设置实例的默认缓存策略 WebRequest 。 该 CachePolicy 属性用于获取或设置特定请求的缓存策略。

仅当检索资源响应流并将其读取到流的末尾时,资源的副本才会添加到缓存中。 因此,针对同一资源的另一个请求可以使用缓存副本,具体取决于此请求的默认缓存策略级别。

适用于

另请参阅