HttpRequestCachePolicy Constructors

Definition

Initializes a new instance of the HttpRequestCachePolicy class.

Overloads

HttpRequestCachePolicy()

Initializes a new instance of the HttpRequestCachePolicy class.

HttpRequestCachePolicy(DateTime)

Initializes a new instance of the HttpRequestCachePolicy class using the specified cache synchronization date.

HttpRequestCachePolicy(HttpRequestCacheLevel)

Initializes a new instance of the HttpRequestCachePolicy class using the specified cache policy.

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan)

Initializes a new instance of the HttpRequestCachePolicy class using the specified age control and time values.

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan)

Initializes a new instance of the HttpRequestCachePolicy class using the specified maximum age, age control value, and time value.

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan, DateTime)

Initializes a new instance of the HttpRequestCachePolicy class using the specified maximum age, age control value, time value, and cache synchronization date.

HttpRequestCachePolicy()

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class.

C#
public HttpRequestCachePolicy();

Examples

The following code example demonstrates setting the cache policy using this constructor.

C#
public static WebResponse GetResponseUsingCacheDefault(Uri uri)
{
    // Set  the default cache policy level for the "http:" scheme.
    RequestCachePolicy policy = new RequestCachePolicy();
    // Create the request.
    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

This constructor initializes the Level property to Default.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HttpRequestCachePolicy(DateTime)

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class using the specified cache synchronization date.

C#
public HttpRequestCachePolicy(DateTime cacheSyncDate);

Parameters

cacheSyncDate
DateTime

A DateTime object that specifies the time when resources stored in the cache must be revalidated.

Examples

The following code example demonstrates creating a cache policy based on a cache synchronization date.

C#
public static HttpRequestCachePolicy CreateLastSyncPolicy(DateTime when)
{
    HttpRequestCachePolicy policy =
       new HttpRequestCachePolicy(when);

    Console.WriteLine("When: {0}", when);
    Console.WriteLine(policy.CacheSyncDate.ToString());
    return policy;
}

Remarks

The cache synchronization date allows you to specify an absolute date when cached contents must be revalidated. If the cache entry was last revalidated prior to the cache synchronization date, revalidation with the server occurs. If the cache entry was revalidated after the cache synchronization date and there are no server revalidation requirements that make the cached entry invalid, the entry from the cache is used. If the cache synchronization date is set to a future date, the entry is revalidated every time it is requested, until the cache synchronization date passes.

This constructor initializes the Level property to Default. The CacheSyncDate property is initialized to cacheSyncDate.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HttpRequestCachePolicy(HttpRequestCacheLevel)

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class using the specified cache policy.

C#
public HttpRequestCachePolicy(System.Net.Cache.HttpRequestCacheLevel level);

Parameters

Examples

The following code example demonstrates creating a cache policy that allows resources found in the cache to be used from the cache.

C#
public static HttpRequestCachePolicy CreateCacheIfAvailablePolicy()
{
    HttpRequestCachePolicy policy =
        new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);

    Console.WriteLine(policy.ToString());
    return policy;
}

Remarks

This constructor initializes the Level property to level.

The HttpRequestCacheLevel value controls whether caching is enabled, and when the cache can be used. For additional information, see the HttpRequestCacheLevel documentation.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan)

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class using the specified age control and time values.

C#
public HttpRequestCachePolicy(System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan ageOrFreshOrStale);

Parameters

cacheAgeControl
HttpCacheAgeControl

One of the following HttpCacheAgeControl enumeration values: MaxAge, MaxStale, or MinFresh.

ageOrFreshOrStale
TimeSpan

A TimeSpan value that specifies an amount of time.

Exceptions

The value specified for the cacheAgeControl parameter cannot be used with this constructor.

Examples

The following code example demonstrates creating a cache policy based on minimum freshness.

C#
public static HttpRequestCachePolicy CreateMinFreshPolicy(TimeSpan span)
{
    HttpRequestCachePolicy policy =
        new HttpRequestCachePolicy(HttpCacheAgeControl.MinFresh, span);
    Console.WriteLine("Minimum freshness {0}", policy.MinFresh.ToString());
    return policy;
}

Remarks

The cacheAgeControl value defines the meaning of the ageOrFreshOrStale parameter value and is used to set the associated property. For example, when you specify MaxStale, the MaxStale property is set to the value of the ageOrFreshOrStale parameter.

This constructor initializes the Level property to Default.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan)

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class using the specified maximum age, age control value, and time value.

C#
public HttpRequestCachePolicy(System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale);

Parameters

cacheAgeControl
HttpCacheAgeControl

An HttpCacheAgeControl value.

maxAge
TimeSpan

A TimeSpan value that specifies the maximum age for resources.

freshOrStale
TimeSpan

A TimeSpan value that specifies an amount of time.

Exceptions

The value specified for the cacheAgeControl parameter is not valid.

Examples

The following code example demonstrates creating a cache policy based on minimum freshness and maximum age.

C#
public static HttpRequestCachePolicy CreateFreshAndAgePolicy(TimeSpan freshMinimum, TimeSpan ageMaximum)
{
    HttpRequestCachePolicy policy =
        new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum);
    Console.WriteLine(policy.ToString());
    return policy;
}

Remarks

The cacheAgeControl value is used to interpret the meaning of the freshOrStale parameter value and set the associated property. For example, when you specify MaxStale, the MaxStale property is set to the value of the freshOrStale parameter. When you specify MaxAgeAndMaxStale, the MaxAge property is set using the value of the maxAge parameter and the MaxStale property is set using the value of the freshOrStale parameter.

Note that unless you specify MaxAgeAndMaxStale or MaxAgeAndMinFresh, the MaxAge property is not set.

This constructor initializes the Level property to Default.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan, DateTime)

Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs
Source:
HttpRequestCachePolicy.cs

Initializes a new instance of the HttpRequestCachePolicy class using the specified maximum age, age control value, time value, and cache synchronization date.

C#
public HttpRequestCachePolicy(System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale, DateTime cacheSyncDate);

Parameters

cacheAgeControl
HttpCacheAgeControl

An HttpCacheAgeControl value.

maxAge
TimeSpan

A TimeSpan value that specifies the maximum age for resources.

freshOrStale
TimeSpan

A TimeSpan value that specifies an amount of time.

cacheSyncDate
DateTime

A DateTime object that specifies the time when resources stored in the cache must be revalidated.

Examples

The following code example demonstrates creating a cache policy based on minimum freshness, maximum age, and a cache synchronization date.

C#
public static HttpRequestCachePolicy CreateFreshAndAgePolicy2(TimeSpan freshMinimum, TimeSpan ageMaximum, DateTime when)
{
    HttpRequestCachePolicy policy =
        new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAgeAndMinFresh, ageMaximum, freshMinimum, when);
    Console.WriteLine(policy.ToString());
    return policy;
    // For the following invocation:
    // CreateFreshAndAgePolicy(new TimeSpan(5,0,0), new TimeSpan(10,0,0),);
    // the output is:
    // Level:Automatic
    // AgeControl:MinFreshAndMaxAge
    // MinFresh:18000
    // MaxAge:36000
}

Remarks

The cacheAgeControl value is used to interpret the meaning of the freshOrStale parameter value and set the associated property. For example, when you specify MaxStale, the MaxStale property is set to the value of the freshOrStale parameter. When you specify MaxAgeAndMaxStale, the MaxAge property is set using the value of the maxAge parameter and the MaxStale property is set using the value of the freshOrStale parameter.

Note that unless you specify MaxAgeAndMaxStale or MaxAgeAndMinFresh, the MaxAge property is not set.

This constructor initializes the CacheSyncDate property to cacheSyncDate, and initializes the Level property to Default.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1