HttpRequestCacheLevel 列舉

定義

指定使用超文字傳輸協定 (Hypertext Transfer Protocol,HTTP) 取得之資源的快取行為。Specifies caching behavior for resources obtained using the Hypertext Transfer protocol (HTTP).

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

欄位

BypassCache 1

使用伺服器滿足要求。Satisfies a request by using the server. 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。No entries are taken from caches, added to caches, or removed from caches between the client and server. 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。No entries are taken from caches, added to caches, or removed from caches between the client and server. 這是電腦組態檔 (隨 .NET Framework 一起提供) 中指定的預設快取行為。This is the default cache behavior specified in the machine configuration file that ships with the .NET Framework.

CacheIfAvailable 3

如果資源可用,則從快取滿足資源要求,否則會將資源要求傳送至伺服器。Satisfies a request for a resource from the cache if the resource is available; otherwise, sends a request for a resource to the server. 如果用戶端與伺服器之間的任何快取中存在要求的項目,則中繼快取可以滿足該要求。If the requested item is available in any cache between the client and the server, the request might be satisfied by the intermediate cache.

CacheOnly 2

使用本機快取的資源滿足要求;不傳送未在快取中的項目要求。Satisfies a request using the locally cached resource; does not send a request for an item that is not in the cache. 指定這個快取原則層級時,如果項目不在用戶端快取中,則會擲回 WebException 例外狀況。When this cache policy level is specified, a WebException exception is thrown if the item is not in the client cache.

CacheOrNextCacheOnly 7

從本機電腦的快取或區域網路上的遠端快取滿足資源要求。Satisfies a request for a resource either from the local computer's cache or a remote cache on the local area network. 如果無法滿足該要求,則會擲回 WebException 例外狀況。If the request cannot be satisfied, a WebException exception is thrown. 在 HTTP 快取通訊協定中,可以使用 only-if-cached 快取控制項指示詞達到此效果。In the HTTP caching protocol, this is achieved using the only-if-cached cache control directive.

Default 0

使用資源的快取複本,或將資源要求傳送至伺服器,以滿足資源要求。Satisfies a request for a resource either by using the cached copy of the resource or by sending a request for the resource to the server. 採取的動作要由目前的快取原則和快取中內容的保留期限決定。The action taken is determined by the current cache policy and the age of the content in the cache. 這是大部分應用程式應當使用的快取層級。This is the cache level that should be used by most applications.

NoCacheNoStore 6

使用快取中的資源永遠無法滿足要求,也不會快取資源。Never satisfies a request by using resources from the cache and does not cache resources. 如果資源出現在本機快取中,則會將其移除。If the resource is present in the local cache, it is removed. 這個原則層級表示中繼快取應當移除資源。This policy level indicates to intermediate caches that they should remove the resource. 在 HTTP 快取通訊協定中,可以使用 no-cache 快取控制項指示詞達到此效果。In the HTTP caching protocol, this is achieved using the no-cache cache control directive.

Refresh 8

使用伺服器或非本機快取的快取滿足要求。Satisfies a request by using the server or a cache other than the local cache. 在中繼快取滿足要求之前,該快取必須重新向伺服器驗證其快取的項目。Before the request can be satisfied by an intermediate cache, that cache must revalidate its cached entry with the server. 在 HTTP 快取通訊協定中,可以使用 max-age = 0 快取控制項指示詞和無快取 Pragma 標頭達到此效果。In the HTTP caching protocol, this is achieved using the max-age = 0 cache control directive and the no-cache Pragma header.

Reload 5

使用伺服器滿足要求。Satisfies a request by using the server. 回應可以儲存在快取中。The response might be saved in the cache. 在 HTTP 快取通訊協定中,可以使用 no-cache 快取控制項指示詞和無快取 Pragma 標頭達到此效果。In the HTTP caching protocol, this is achieved using the no-cache cache control directive and the no-cache Pragma header.

Revalidate 4

比較快取中的資源複本和伺服器上的複本。Compares the copy of the resource in the cache with the copy on the server. 如果伺服器上的複本較新,就會用它來滿足要求,並取代快取中的複本。If the copy on the server is newer, it is used to satisfy the request and replaces the copy in the cache. 如果快取中的複本和伺服器上的版本相同,就使用快取的複本。If the copy in the cache is the same as the server copy, the cached copy is used. 在 HTTP 快取通訊協定中,使用條件式要求即可達成。In the HTTP caching protocol, this is achieved using a conditional request.

範例

下列程式碼範例會將應用程式域的快取原則設定為預設值。The following code example sets the application domain's caching policy to 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 物件所指定的快取層級。This enumeration is used to set the cache level specified by HttpRequestCachePolicy objects.

這個 BypassCache 值是 .NET Framework 隨附之電腦設定檔中所指定的預設快取行為。This BypassCache value is the default cache behavior specified in the machine configuration file that ships with the .NET Framework. 用戶端與伺服器之間沒有任何項目取得自快取、加入快取或從快取移除。No entries are taken from caches, added to caches, or removed from caches between the client and server.

HttpWebRequest.DefaultCachePolicy 屬性是用來取得或設定 HttpWebRequest 實例的預設快取原則。The HttpWebRequest.DefaultCachePolicy property is used to get or set the default cache policy for HttpWebRequest instances. WebRequest.DefaultCachePolicy 屬性是用來取得或設定 WebRequest 實例的預設快取原則。The WebRequest.DefaultCachePolicy property is used to get or set the default cache policy for a WebRequest instance. CachePolicy 屬性是用來取得或設定特定要求的快取原則。The CachePolicy property is used to get or set the cache policy for a specific request.

只有在抓取資源的回應資料流程並讀取至資料流程結尾時,才會將資源複本新增至快取。A copy of a resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. 因此,相同資源的另一個要求可以使用快取的複本,視此要求的預設快取原則層級而定。So another request for the same resource could use a cached copy, depending on the default cache policy level for this request.

適用於

另請參閱