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()

Initializes a new instance of the HttpRequestCachePolicy class.

public:
 HttpRequestCachePolicy();
public HttpRequestCachePolicy ();
Public Sub New ()

Examples

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

static WebResponse^ GetResponseUsingCacheDefault( Uri^ uri )
{
   // Set  the default cache policy level for the "http:" scheme.
   RequestCachePolicy^ policy = gcnew RequestCachePolicy;

   // Create the request.
   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 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

HttpRequestCachePolicy(DateTime)

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

public:
 HttpRequestCachePolicy(DateTime cacheSyncDate);
public HttpRequestCachePolicy (DateTime cacheSyncDate);
new System.Net.Cache.HttpRequestCachePolicy : DateTime -> System.Net.Cache.HttpRequestCachePolicy
Public Sub New (cacheSyncDate As DateTime)

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.

static HttpRequestCachePolicy^ CreateLastSyncPolicy( DateTime when )
{
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( when );
   Console::WriteLine( L"When: {0}", when );
   Console::WriteLine( policy->CacheSyncDate );
   return policy;
}
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

HttpRequestCachePolicy(HttpRequestCacheLevel)

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

public:
 HttpRequestCachePolicy(System::Net::Cache::HttpRequestCacheLevel level);
public HttpRequestCachePolicy (System.Net.Cache.HttpRequestCacheLevel level);
new System.Net.Cache.HttpRequestCachePolicy : System.Net.Cache.HttpRequestCacheLevel -> System.Net.Cache.HttpRequestCachePolicy
Public Sub New (level As HttpRequestCacheLevel)

Parameters

Examples

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

static HttpRequestCachePolicy^ CreateCacheIfAvailablePolicy()
{
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::CacheIfAvailable );
   Console::WriteLine( policy );
   return policy;
}
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

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan)

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

public:
 HttpRequestCachePolicy(System::Net::Cache::HttpCacheAgeControl cacheAgeControl, TimeSpan ageOrFreshOrStale);
public HttpRequestCachePolicy (System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan ageOrFreshOrStale);
new System.Net.Cache.HttpRequestCachePolicy : System.Net.Cache.HttpCacheAgeControl * TimeSpan -> System.Net.Cache.HttpRequestCachePolicy
Public Sub New (cacheAgeControl As HttpCacheAgeControl, ageOrFreshOrStale As TimeSpan)

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.

static HttpRequestCachePolicy^ CreateMinFreshPolicy( TimeSpan span )
{
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpCacheAgeControl::MinFresh,span );
   Console::WriteLine( L"Minimum freshness {0}", policy->MinFresh );
   return policy;
}
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

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan)

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

public:
 HttpRequestCachePolicy(System::Net::Cache::HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale);
public HttpRequestCachePolicy (System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale);
new System.Net.Cache.HttpRequestCachePolicy : System.Net.Cache.HttpCacheAgeControl * TimeSpan * TimeSpan -> System.Net.Cache.HttpRequestCachePolicy
Public Sub New (cacheAgeControl As HttpCacheAgeControl, maxAge As TimeSpan, freshOrStale As TimeSpan)

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.

static HttpRequestCachePolicy^ CreateFreshAndAgePolicy( TimeSpan freshMinimum, TimeSpan ageMaximum )
{
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpCacheAgeControl::MaxAgeAndMinFresh,
       ageMaximum, freshMinimum );
   Console::WriteLine( policy );
   return policy;
}
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

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.

public:
 HttpRequestCachePolicy(System::Net::Cache::HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale, DateTime cacheSyncDate);
public HttpRequestCachePolicy (System.Net.Cache.HttpCacheAgeControl cacheAgeControl, TimeSpan maxAge, TimeSpan freshOrStale, DateTime cacheSyncDate);
new System.Net.Cache.HttpRequestCachePolicy : System.Net.Cache.HttpCacheAgeControl * TimeSpan * TimeSpan * DateTime -> System.Net.Cache.HttpRequestCachePolicy
Public Sub New (cacheAgeControl As HttpCacheAgeControl, maxAge As TimeSpan, freshOrStale As TimeSpan, cacheSyncDate As DateTime)

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.

static HttpRequestCachePolicy^ CreateFreshAndAgePolicy2( TimeSpan freshMinimum, TimeSpan ageMaximum, DateTime when )
{
   HttpRequestCachePolicy^ policy = 
       gcnew HttpRequestCachePolicy( HttpCacheAgeControl::MaxAgeAndMinFresh, 
       ageMaximum, freshMinimum, when );
   Console::WriteLine( policy );
   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
}
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