RequestCacheLevel Enum

Definition

Specifies caching behavior for resources obtained using WebRequest and its derived classes.

public enum class RequestCacheLevel
public enum RequestCacheLevel
type RequestCacheLevel = 
Public Enum RequestCacheLevel
Inheritance
RequestCacheLevel

Fields

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

Examples

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

Remarks

Members of this enumeration are used to initialize RequestCachePolicy objects. The current setting for a RequestCachePolicy object is available in the HttpRequestCachePolicy.Level property.

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.

The HttpWebRequest.DefaultCachePolicy property is used to get or set the default cache policy for HttpWebRequest instances. The WebRequest.CachePolicy property is used to get or set the default cache policy for a WebRequest instances. The WebRequest.CachePolicy property is used to get or set the cache policy for a specific request.

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. With CacheIfAvailable, subsequent requests for the same resource would use a cached copy. 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.

Applies to

See also