HttpRequestCacheLevel Enumeration

Specifies caching behavior for resources obtained using the Hypertext Transfer protocol (HTTP).

Namespace:  System.Net.Cache
Assembly:  System (in System.dll)

public enum HttpRequestCacheLevel

Member nameDescription
DefaultSatisfies 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.
BypassCacheSatisfies 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.
CacheOnlySatisfies 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.
CacheIfAvailableSatisfies 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.
RevalidateCompares 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. In the HTTP caching protocol, this is achieved using a conditional request.
ReloadSatisfies 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.
NoCacheNoStoreNever 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.
CacheOrNextCacheOnlySatisfies a request for a resource either from the local computer's cache or a remote cache on the local area network. If the request cannot be satisfied, a WebException exception is thrown. In the HTTP caching protocol, this is achieved using the only-if-cached cache control directive.
RefreshSatisfies 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. In the HTTP caching protocol, this is achieved using the max-age = 0 cache control directive and the no-cache Pragma header.

This enumeration is used to set the cache level specified by HttpRequestCachePolicy objects.

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.

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.

The following code example sets the application domain's caching policy to Default.

// 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 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(S"IsFromCache? {0}", __box(response->IsFromCache));  
        return response;
    }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft