HttpRequestCacheLevel 列舉

定義

指定使用超文字傳輸協定 (Hypertext Transfer Protocol,HTTP) 取得之資源的快取行為。

public enum class HttpRequestCacheLevel
public enum HttpRequestCacheLevel
type HttpRequestCacheLevel = 
Public Enum HttpRequestCacheLevel
繼承
HttpRequestCacheLevel

欄位

BypassCache 1

使用伺服器滿足要求。 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。 這是電腦組態檔 (隨 .NET Framework 一起提供) 中指定的預設快取行為。

CacheIfAvailable 3

如果資源可用,則從快取滿足資源要求,否則會將資源要求傳送至伺服器。 如果用戶端與伺服器之間的任何快取中存在要求的項目,則中繼快取可以滿足該要求。

CacheOnly 2

使用本機快取的資源滿足要求;不傳送未在快取中的項目要求。 指定這個快取原則層級時,如果項目不在用戶端快取中,則會擲回 WebException 例外狀況。

CacheOrNextCacheOnly 7

從本機電腦的快取或區域網路上的遠端快取滿足資源要求。 如果無法滿足該要求,則會擲回 WebException 例外狀況。 在 HTTP 快取通訊協定中,可以使用 only-if-cached 快取控制項指示詞達到此效果。

Default 0

使用資源的快取複本,或將資源要求傳送至伺服器,以滿足資源要求。 採取的動作要由目前的快取原則和快取中內容的保留期限決定。 這是大部分應用程式應當使用的快取層級。

NoCacheNoStore 6

使用快取中的資源永遠無法滿足要求,也不會快取資源。 如果資源出現在本機快取中,則會將其移除。 這個原則層級表示中繼快取應當移除資源。 在 HTTP 快取通訊協定中,可以使用 no-cache 快取控制項指示詞達到此效果。

Refresh 8

使用伺服器或非本機快取的快取滿足要求。 在中繼快取滿足要求之前,該快取必須重新向伺服器驗證其快取的項目。 在 HTTP 快取通訊協定中,可以使用 max-age = 0 快取控制項指示詞和無快取 Pragma 標頭達到此效果。

Reload 5

使用伺服器滿足要求。 回應可以儲存在快取中。 在 HTTP 快取通訊協定中,可以使用 no-cache 快取控制項指示詞和無快取 Pragma 標頭達到此效果。

Revalidate 4

比較快取中的資源複本和伺服器上的複本。 如果伺服器上的複本較新,就會用它來滿足要求,並取代快取中的複本。 如果快取中的複本和伺服器上的版本相同,就使用快取的複本。 在 HTTP 快取通訊協定中,使用條件式要求即可達成。

範例

下列程式碼範例會將應用程式域的快取原則設定為 Default。

// The following method demonstrates overriding the
// caching policy for a request.
static WebResponse^ GetResponseNoCache( Uri^ uri )
{
   // Set a default policy level for the "http:" and "https" schemes.
   HttpRequestCachePolicy^ policy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::Default );
   HttpWebRequest::DefaultCachePolicy = policy;

   // Create the request.
   WebRequest^ request = WebRequest::Create( uri );

   // Define a cache policy for this request only. 
   HttpRequestCachePolicy^ noCachePolicy = gcnew HttpRequestCachePolicy( HttpRequestCacheLevel::NoCacheNoStore );
   request->CachePolicy = noCachePolicy;
   WebResponse^ response = request->GetResponse();
   Console::WriteLine( L"IsFromCache? {0}", response->IsFromCache );
   
   return response;
}
// The following method demonstrates overriding the
// caching policy for a request.
public static WebResponse GetResponseNoCache(Uri uri)
{
    // Set a default policy level for the "http:" and "https" schemes.
    HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
    HttpWebRequest.DefaultCachePolicy = policy;
    // Create the request.
    WebRequest request = WebRequest.Create(uri);
    // Define a cache policy for this request only.
    HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
    request.CachePolicy = noCachePolicy;
    WebResponse response = request.GetResponse();
    Console.WriteLine("IsFromCache? {0}", response.IsFromCache);
    return response;
}

備註

這個列舉是用來設定 物件所 HttpRequestCachePolicy 指定的快取層級。

這個 BypassCache 值是隨附于.NET Framework之機器組態檔中指定的預設快取行為。 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。

屬性 HttpWebRequest.DefaultCachePolicy 是用來取得或設定實例的預設快取原則 HttpWebRequest 。 屬性 WebRequest.DefaultCachePolicy 是用來取得或設定實例的預設快取原則 WebRequest 。 屬性 CachePolicy 是用來取得或設定特定要求的快取原則。

只有在擷取並讀取至資料流程的回應資料流程時,才會將資源的複本新增至快取。 因此,相同資源的另一個要求可以使用快取複本,視此要求的預設快取原則層級而定。

適用於

另請參閱