How to: customize a time-based cache policy

When creating a time-based cache policy, you can customize caching behavior by specifying values for maximum age, minimum freshness, maximum staleness, or cache synchronization date. The HttpRequestCachePolicy object provides several constructors that allow you to specify valid combinations of these values.

To create a time-based cache policy that uses a cache synchronization date

Create a time-based cache policy that uses a cache synchronization date by passing a DateTime object to the HttpRequestCachePolicy constructor:

public static HttpRequestCachePolicy CreateLastSyncPolicy(DateTime when)
{
    var policy = new HttpRequestCachePolicy(when);
    Console.WriteLine("When: {0}", when);
    Console.WriteLine(policy.ToString());
    return policy;
}
Public Shared Function CreateLastSyncPolicy([when] As DateTime) As HttpRequestCachePolicy
    Dim policy As New HttpRequestCachePolicy([when])
    Console.WriteLine("When: {0}", [when])
    Console.WriteLine(policy.ToString())
    Return policy
End Function

The output is similar to the following:

When: 1/14/2004 8:07:30 AM
Level:Default CacheSyncDate:1/14/2004 8:07:30 AM

To create a time-based cache policy that is based on minimum freshness

Create a time-based cache policy that is based on minimum freshness by specifying MinFresh as the cacheAgeControl parameter value and passing a TimeSpan object to the HttpRequestCachePolicy constructor:

public static HttpRequestCachePolicy CreateMinFreshPolicy(TimeSpan span)
{
    var policy = new HttpRequestCachePolicy(HttpCacheAgeControl.MinFresh, span);
    Console.WriteLine(policy.ToString());
    return policy;
}
Public Shared Function CreateMinFreshPolicy(span As TimeSpan) As HttpRequestCachePolicy
    Dim policy As New HttpRequestCachePolicy(HttpCacheAgeControl.MinFresh, span)
    Console.WriteLine(policy.ToString())
    Return policy
End Function

For the following invocation:

CreateMinFreshPolicy(new TimeSpan(1,0,0));

The output is:

Level:Default MinFresh:3600

To create a time-based cache policy that is based on minimum freshness and maximum age

Create a time-based cache policy that is based on minimum freshness and maximum age by specifying MaxAgeAndMinFresh as the cacheAgeControl parameter value and passing two TimeSpan objects to the HttpRequestCachePolicy constructor, one to specify the maximum age for resources and a second to specify the minimum freshness permitted for an object returned from the cache:

public static HttpRequestCachePolicy CreateFreshAndAgePolicy(TimeSpan freshMinimum, TimeSpan ageMaximum)
{
    var policy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum);
    Console.WriteLine(policy.ToString());
    return policy;
}
Public Shared Function CreateFreshAndAgePolicy(freshMinimum As TimeSpan, ageMaximum As TimeSpan) As HttpRequestCachePolicy
    Dim policy As New HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum)
    Console.WriteLine(policy.ToString())
    Return policy
End Function

For the following invocation:

CreateFreshAndAgePolicy(new TimeSpan(5,0,0), new TimeSpan(10,0,0));  

The output is:

Level:Default MaxAge:36000 MinFresh:18000  

See also