Cache Cache Cache Cache Class

定義

實作 Web 應用程式的快取。Implements the cache for a Web application. 這個類別無法被繼承。This class cannot be inherited.

public ref class Cache sealed : System::Collections::IEnumerable
public sealed class Cache : System.Collections.IEnumerable
type Cache = class
    interface IEnumerable
Public NotInheritable Class Cache
Implements IEnumerable
繼承
CacheCacheCacheCache
實作

範例

下列範例會快取中的項目指派的值會顯示使用者的頁面,並再從快取中移除項目時通知他們。The following example is a page that shows users the value assigned to an item in the cache, and then notifies them when the item is removed from the cache. 它會建立RemovedCallback方法,其中有簽章的CacheItemRemovedCallback委派,會移除快取項目,並使用時通知使用者CacheItemRemovedReason告訴他們移除原因的列舉。It creates a RemovedCallback method, which has the signature of the CacheItemRemovedCallback delegate, to notify users when the cache item is removed, and it uses the CacheItemRemovedReason enumeration to tell them why it was removed. 此外,它會使用Cache.Item[String]屬性將物件加入至快取,並擷取這些物件的值。In addition, it uses the Cache.Item[String] property to add objects to the cache and retrieve the value of those objects. AddItemToCache方法,它會使用Cache.Add方法新增至快取的項目。In the AddItemToCache method, it uses the Cache.Add method to add an item to the cache. 若要使用CacheItemRemovedCallback委派,必須使用此方法將項目新增至快取或Cache.Insert方法,因此,ASP.NET 可以自動進行適當的方法呼叫時移除的項目。To use the CacheItemRemovedCallback delegate, you must add an item to the cache with this method or the Cache.Insert method, so that that ASP.NET can automatically make the proper method calls when the item is removed. 自訂RemoveItemFromCache方法會使用Cache.Remove方法來明確地從快取中刪除項目造成RemovedCallback要叫用方法。The custom RemoveItemFromCache method uses the Cache.Remove method to explicitly delete the item from the cache, causing the RemovedCallback method to be invoked.

注意

由下列程式碼片段存取快取的執行個體屬於Page這個範例會繼承的物件。The instance of Cache that is accessed by the snippet below is a member of the Page object that this sample inherits.

<html>
 <Script runat=server language="C#">
// <snippet2>
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
    }
// </snippet2>

// <snippet3>
    public void AddItemToCache(Object sender, EventArgs e) {
        itemRemoved = false;

        onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

        if (Cache["Key1"] == null)
          Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
    }
// </snippet3>

// <snippet4>
    public void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
// </snippet4>
 </Script>
 <body>
  <Form runat="server">
   <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
   <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
  <% if (itemRemoved) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("<BR>");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     }
     else {
// <snippet5>
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
// </snippet5>
     }
  %>
 </body>
</html>
<%@ Page Language="VB" %>

<html>
 <Script runat=server>
' <snippet2>
    Shared itemRemoved As boolean = false
    Shared reason As CacheItemRemovedReason
    Dim onRemove As CacheItemRemovedCallback

    Public Sub RemovedCallback(k As String, v As Object, r As CacheItemRemovedReason)
      itemRemoved = true
      reason = r
    End Sub
' </snippet2>

' <snippet3>
    Public Sub AddItemToCache(sender As Object, e As EventArgs)
        itemRemoved = false

        onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

        If (IsNothing(Cache("Key1"))) Then
          Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
        End If
    End Sub
' </snippet3>

' <snippet4>
    Public Sub RemoveItemFromCache(sender As Object, e As EventArgs)
        If (Not IsNothing(Cache("Key1"))) Then
          Cache.Remove("Key1")
        End If
    End Sub
' </snippet4>
 </Script>

 <body>
  <Form runat="server">
    <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
    <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
<%
If (itemRemoved) Then
    Response.Write("RemovedCallback event raised.")
    Response.Write("<BR>")
    Response.Write("Reason: <B>" + reason.ToString() + "</B>")
Else
' <snippet5>
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
' </snippet5>
End If
%>
 </body>
</html>

備註

一個執行個體,這個類別會建立每個應用程式網域,它會維持有效狀態,只要應用程式定義域會維持使用中。One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. 此類別的執行個體的相關資訊可透過Cache的屬性HttpContext物件或Cache屬性Page物件。Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

注意

Cache類別並不適用於 ASP.NET 應用程式以外的使用。The Cache class is not intended for use outside of ASP.NET applications. 設計和測試以提供 Web 應用程式的快取的 ASP.NET 中使用。It was designed and tested for use in ASP.NET to provide caching for Web applications. 對於其他類型的應用程式,例如主控台應用程式或 Windows Forms 應用程式,使用ObjectCache類別。For other types of applications, such as console applications or Windows Forms applications, use the ObjectCache class.

建構函式

Cache() Cache() Cache() Cache()

初始化 Cache 類別的新執行個體。Initializes a new instance of the Cache class.

欄位

NoAbsoluteExpiration NoAbsoluteExpiration NoAbsoluteExpiration NoAbsoluteExpiration

Insert(String, Object) 方法呼叫中的 absoluteExpiration 參數中使用,以指示該項目應該永不過期。Used in the absoluteExpiration parameter in an Insert(String, Object) method call to indicate the item should never expire. 此欄位為唯讀。This field is read-only.

NoSlidingExpiration NoSlidingExpiration NoSlidingExpiration NoSlidingExpiration

使用來做為 Insert(String, Object)Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) 方法呼叫中的 slidingExpiration 參數以停用變動性期限。Used as the slidingExpiration parameter in an Insert(String, Object) or Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) method call to disable sliding expirations. 此欄位為唯讀。This field is read-only.

屬性

Count Count Count Count

取得快取中所儲存的項目數。Gets the number of items stored in the cache.

EffectivePercentagePhysicalMemoryLimit EffectivePercentagePhysicalMemoryLimit EffectivePercentagePhysicalMemoryLimit EffectivePercentagePhysicalMemoryLimit

取得應用程式可以消耗的實體記憶體百分比,超過這個百分比,ASP.NET 就會開始移除快取區中的項目。Gets the percentage of physical memory that can be consumed by an application before ASP.NET starts removing items from the cache.

EffectivePrivateBytesLimit EffectivePrivateBytesLimit EffectivePrivateBytesLimit EffectivePrivateBytesLimit

取得快取可用的位元組數目。Gets the number of bytes available for the cache.

Item[String] Item[String] Item[String] Item[String]

取得或設定在指定索引鍵的快取項目。Gets or sets the cache item at the specified key.

方法

Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

使用相依性、期限和優先權原則,將指定項目加入 Cache 物件,並且您也可以從 Cache 移除插入項目時使用委派 (Delegate) 來告知您的應用程式。Adds the specified item to the Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Get(String) Get(String) Get(String) Get(String)

Cache 擷取指定的項目。Retrieves the specified item from the Cache object.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

擷取字典列舉程式,用來逐一查看金鑰設定及其在快取中所包含的值。Retrieves a dictionary enumerator used to iterate through the key settings and their values contained in the cache.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
Insert(String, Object) Insert(String, Object) Insert(String, Object) Insert(String, Object)

使用 Cache 列舉型別所提供的預設值,將項目插入 CacheItemPriority 物件,並讓快取索引鍵參考其位置。Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency)

將物件插入至有檔案或索引鍵相依性的 CacheInserts an object into the Cache that has file or key dependencies.

Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan)

使用相依性和期限原則將物件插入 CacheInserts an object into the Cache with dependencies and expiration policies.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

使用相依性、期限和優先權原則,將指定項目插入 Cache 物件,並且您也可以從 Cache 移除插入項目時使用委派來告知您的應用程式。Inserts an object into the Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

從快取區移除項目之前,將物件連同相依性、期限原則及可用來通知應用程式的委派一併插入 Cache 物件中。Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String) Remove(String) Remove(String) Remove(String)

從應用程式的 Cache 物件移除指定的項目。Removes the specified item from the application's Cache object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

傳回列舉值,可逐一查看 Cache 物件集合。Returns an enumerator that can iterate through the Cache object collection.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

執行緒安全性

此型別具備執行緒安全。This type is thread safe.

另請參閱