HttpRequestCachePolicy 构造函数

定义

初始化 HttpRequestCachePolicy 类的新实例。

重载

HttpRequestCachePolicy()

初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy(DateTime)

使用指定的缓存同步日期初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy(HttpRequestCacheLevel)

使用指定的缓存策略初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan)

使用指定的生存期控件和时间值初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan)

使用指定的最大生存期、生存期控制值和时间值初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan, DateTime)

使用指定的最大生存期、生存期控制值、时间值和缓存同步日期初始化 HttpRequestCachePolicy 类的新实例。

HttpRequestCachePolicy()

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

初始化 HttpRequestCachePolicy 类的新实例。

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

示例

下面的代码示例演示如何使用此构造函数设置缓存策略。

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;
}

注解

此构造函数将 Level 属性初始化为 Default

适用于

HttpRequestCachePolicy(DateTime)

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

使用指定的缓存同步日期初始化 HttpRequestCachePolicy 类的新实例。

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

参数

cacheSyncDate
DateTime

一个 DateTime 对象,它指定必须重新验证缓存中存储的资源的时间。

示例

下面的代码示例演示如何基于缓存同步日期创建缓存策略。

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;
}

注解

缓存同步日期允许指定必须重新验证缓存内容的绝对日期。 如果缓存条目在缓存同步日期之前进行了最后一次重新验证,则会对服务器重新验证。 如果缓存条目在缓存同步日期之后重新验证,并且没有服务器重新验证要求使缓存项无效,则使用缓存中的条目。 如果缓存同步日期设置为未来某个日期,则每次请求时都会重新验证该条目,直到缓存同步日期过去。

此构造函数将 Level 属性初始化为 Default。 将 CacheSyncDate 属性初始化为 cacheSyncDate

适用于

HttpRequestCachePolicy(HttpRequestCacheLevel)

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

使用指定的缓存策略初始化 HttpRequestCachePolicy 类的新实例。

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)

参数

示例

下面的代码示例演示如何创建一个缓存策略,该策略允许从缓存中使用缓存中找到的资源。

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;
}

注解

此构造函数将 Level 属性初始化为 level

HttpRequestCacheLevel 控制是否启用缓存以及何时可以使用缓存。 有关其他信息, HttpRequestCacheLevel 请参阅文档。

适用于

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan)

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

使用指定的生存期控件和时间值初始化 HttpRequestCachePolicy 类的新实例。

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)

参数

cacheAgeControl
HttpCacheAgeControl

以下 HttpCacheAgeControl 枚举值之一:MaxAgeMaxStaleMinFresh

ageOrFreshOrStale
TimeSpan

指定时间量的 TimeSpan 值。

例外

cacheAgeControl 参数指定的值不能与此构造函数一起使用。

示例

下面的代码示例演示如何基于最小新鲜度创建缓存策略。

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;
}

注解

cacheAgeControl 定义参数值的含义 ageOrFreshOrStale ,并用于设置关联的属性。 例如,指定 MaxStale时, MaxStale 属性设置为 参数的值 ageOrFreshOrStale

此构造函数将 Level 属性初始化为 Default

适用于

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan)

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

使用指定的最大生存期、生存期控制值和时间值初始化 HttpRequestCachePolicy 类的新实例。

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)

参数

cacheAgeControl
HttpCacheAgeControl

一个 HttpCacheAgeControl 值。

maxAge
TimeSpan

指定资源最大生存期的 TimeSpan 值。

freshOrStale
TimeSpan

指定时间量的 TimeSpan 值。

例外

cacheAgeControl 参数指定的值无效。

示例

下面的代码示例演示如何创建基于最小新鲜度和最长生存期的缓存策略。

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;
}

注解

cacheAgeControl 用于解释参数值的含义 freshOrStale 并设置关联的属性。 例如,指定 MaxStale时, MaxStale 属性设置为 参数的值 freshOrStale 。 指定 MaxAgeAndMaxStale时,将使用 MaxAge 参数的值 maxAge 设置 属性, MaxStale 并使用 参数的值 freshOrStale 设置 属性。

请注意,除非指定 MaxAgeAndMaxStaleMaxAgeAndMinFreshMaxAge 否则不会设置 属性。

此构造函数将 Level 属性初始化为 Default

适用于

HttpRequestCachePolicy(HttpCacheAgeControl, TimeSpan, TimeSpan, DateTime)

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

使用指定的最大生存期、生存期控制值、时间值和缓存同步日期初始化 HttpRequestCachePolicy 类的新实例。

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)

参数

cacheAgeControl
HttpCacheAgeControl

一个 HttpCacheAgeControl 值。

maxAge
TimeSpan

指定资源最大生存期的 TimeSpan 值。

freshOrStale
TimeSpan

指定时间量的 TimeSpan 值。

cacheSyncDate
DateTime

一个 DateTime 对象,它指定必须重新验证缓存中存储的资源的时间。

示例

下面的代码示例演示如何创建基于最小新鲜度、最长期限和缓存同步日期的缓存策略。

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
}

注解

cacheAgeControl 用于解释参数值的含义 freshOrStale 并设置关联的属性。 例如,指定 MaxStale时, MaxStale 属性设置为 参数的值 freshOrStale 。 指定 MaxAgeAndMaxStale时,将使用 MaxAge 参数的值 maxAge 设置 属性, MaxStale 并使用 参数的值 freshOrStale 设置 属性。

请注意,除非指定 MaxAgeAndMaxStaleMaxAgeAndMinFreshMaxAge 否则不会设置 属性。

此构造函数将 CacheSyncDate 属性初始化为 cacheSyncDate,并将 属性初始化 LevelDefault

适用于