Compartilhar via


How to: Set a Location-Based Cache Policy for an Application

Location-based cache policies allow an application to explicitly define caching behavior based on the location of the requested resource. This topic demonstrates setting the cache policy programmatically. For information on setting the policy for an application using the configuration files, see <requestCaching> Element (Network Settings).

To set a location-based cache policy for an application

  1. Create a RequestCachePolicy or HttpRequestCachePolicy object.

  2. Set the policy object as the default for the application domain.

To set a policy that takes requested resources from a cache

  • Create a policy that takes requested resources from a cache if available, and otherwise, sends requests to the server, by setting the cache level to CacheIfAvailable. A request can be fulfilled by any cache between the client and server, including remote caches.

    public static void UseCacheIfAvailable()
    {
        HttpRequestCachePolicy policy = new HttpRequestCachePolicy
            (HttpRequestCacheLevel.CacheIfAvailable);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub UseCacheIfAvailable()
        Dim policy As New HttpRequestCachePolicy _
             (HttpRequestCacheLevel.CacheIfAvailable)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

To set a policy that prevents any cache from supplying resources

  • Create a policy that prevents any cache from supplying requested resources by setting the cache level to NoCacheNoStore. This policy level removes the resource from the local cache if it is present and indicates to remote caches that they should also remove the resource.

    public static void DoNotUseCache()
    {
    HttpRequestCachePolicy policy = new HttpRequestCachePolicy 
            (HttpRequestCacheLevel.NoCacheNoStore);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub DoNotUseCache()
        Dim policy As New HttpRequestCachePolicy _
            (HttpRequestCacheLevel.NoCacheNoStore)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

To set a policy that returns requested resources only if they are in the local cache

  • Create a policy that returns requested resources only if they are in the local cache by setting the cache level to CacheOnly. If the requested resource is not in the cache, a WebException exception is thrown.

    public static void OnlyUseCache()
    {
        HttpRequestCachePolicy policy = new HttpRequestCachePolicy 
            (HttpRequestCacheLevel.CacheOnly);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub OnlyUseCache()
        Dim policy As New HttpRequestCachePolicy _
            (HttpRequestCacheLevel.CacheOnly)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

To set a policy that prevents the local cache from supplying resources

  • Create a policy that prevents the local cache from supplying requested resources by setting the cache level to Refresh. If the requested resource is in an intermediate cache and is successfully revalidated, the intermediate cache can supply the requested resource.

    public static void DoNotUseLocalCache()
    {
     HttpRequestCachePolicy policy = new HttpRequestCachePolicy 
            (HttpRequestCacheLevel.Refresh);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub DoNotUseLocalCache()
        Dim policy As New HttpRequestCachePolicy _
            (HttpRequestCacheLevel.Refresh)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

To set a policy that prevents any cache from supplying requested resources

  • Create a policy that prevents any cache from supplying requested resources by setting the cache level to Reload. The resource returned by the server can be stored in the cache.

    public static void SendToServer()
    {
    HttpRequestCachePolicy policy = new HttpRequestCachePolicy 
            (HttpRequestCacheLevel.Reload);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub SendToServer()
        Dim policy As New HttpRequestCachePolicy _
            (HttpRequestCacheLevel.Reload)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

To set a policy that allows any cache to supply requested resources if the resource on the server is not newer than the cached copy

  • Create a policy that allows any cache to supply requested resources if the resource on the server is not newer than the cached copy by setting the cache level to Revalidate.

    public static void CheckServer()
    {
    HttpRequestCachePolicy policy = new HttpRequestCachePolicy
             (HttpRequestCacheLevel.Revalidate);
        HttpWebRequest.DefaultCachePolicy = policy;
    }
    
    Public Shared Sub CheckServer()
        Dim policy As New HttpRequestCachePolicy _
            (HttpRequestCacheLevel.Revalidate)
        HttpWebRequest.DefaultCachePolicy = policy
    End Sub
    

See Also

Reference

<requestCaching> Element (Network Settings)

Concepts

Cache Policy
Location-Based Cache Policies
Time-Based Cache Policies

Other Resources

Cache Management for Network Applications