RequestCacheLevel 列舉

定義

指定使用 WebRequest 及其衍生類別取得之資源的快取行為。Specifies caching behavior for resources obtained using WebRequest and its derived classes.

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

欄位

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. 這是電腦組態檔 (隨 .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.

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.

Reload 5

使用伺服器滿足要求。Satisfies a request by using the server. 回應可以儲存在快取中。The response might be saved in the cache. 在 HTTP 快取通訊協定中,可以使用 no-cache 快取控制項指示詞和 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

如果時間戳記與伺服器上資源的時間戳記相同,則使用資源的快取複本滿足要求,否則,會從伺服器下載該資源,將其呈現給呼叫端,並儲存在快取中。Satisfies a request by using the cached copy of the resource if the timestamp is the same as the timestamp of the resource on the server; otherwise, the resource is downloaded from the server, presented to the caller, and stored in the cache.

範例

下列程式碼範例會建立只有在快取中才會傳回資源的原則。The following code example creates policy that returns a resource only if it is in the cache.

static WebResponse^ GetResponseFromCache( Uri^ uri )
{
   RequestCachePolicy^ policy = gcnew RequestCachePolicy( RequestCacheLevel::CacheOnly );
   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 GetResponseFromCache(Uri uri)
{
     RequestCachePolicy policy = 
        new  RequestCachePolicy( RequestCacheLevel.CacheOnly);
    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;
} 

備註

此列舉的成員是用來初始化 RequestCachePolicy 物件。Members of this enumeration are used to initialize RequestCachePolicy objects. RequestCachePolicy 物件的目前設定可在 HttpRequestCachePolicy.Level 屬性中取得。The current setting for a RequestCachePolicy object is available in the HttpRequestCachePolicy.Level property.

這個 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.CachePolicy 屬性是用來取得或設定 WebRequest 實例的預設快取原則。The WebRequest.CachePolicy property is used to get or set the default cache policy for a WebRequest instances. WebRequest.CachePolicy 屬性是用來取得或設定特定要求的快取原則。The WebRequest.CachePolicy property is used to get or set the cache policy for a specific request.

如果快取行為 CacheIfAvailableRevalidate,則只有在取得資源的回應資料流程並讀取至資料流程結尾時,才會將要求的資源複本新增至快取。If the cache behavior is CacheIfAvailable or Revalidate, a copy of a requested resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. 使用 CacheIfAvailable,相同資源的後續要求會使用快取複本。With CacheIfAvailable, subsequent requests for the same resource would use a cached copy. 使用 Revalidate時,如果快取資源的時間戳記與伺服器上資源的時間戳記相同,則相同資源的後續要求會使用快取複本。With Revalidate, subsequent requests for the same resource would use a cached copy if the timestamp for the cached resource is the same as the timestamp of the resource on the server.

只有在抓取資源的回應資料流程並讀取至資料流程結尾時,才會將資源複本新增至快取。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.

適用於

另請參閱