MemoryCache 類別

定義

表示可實作記憶體內部快取的類型。Represents the type that implements an in-memory cache.

public ref class MemoryCache : System::Runtime::Caching::ObjectCache, IDisposable
public class MemoryCache : System.Runtime.Caching.ObjectCache, IDisposable
type MemoryCache = class
    inherit ObjectCache
    interface IEnumerable
    interface IDisposable
Public Class MemoryCache
Inherits ObjectCache
Implements IDisposable
繼承
MemoryCache
實作

範例

下列範例會宣告預設記憶體快取實例的參考。The following example declares a reference to the default memory cache instance. 快取專案會使用 CacheItemPolicy 物件,為快取專案提供收回和到期的詳細資料。The cache entry uses a CacheItemPolicy object to provide eviction and expiration details for the cache entry. 它也會使用 ChangeMonitor 物件來監視檔案系統上來源資料的狀態(也就是檔案)。It also uses a ChangeMonitor object to monitor the state of the source data (which is a file) on the file system.

Private Sub btnGet_Click(ByVal sender As Object, ByVal e As EventArgs)   
    Dim cache As ObjectCache = MemoryCache.[Default]   
    Dim fileContents As String = TryCast(cache("filecontents"), String)   
  
    If fileContents Is Nothing Then   
        Dim policy As New CacheItemPolicy()   
        Dim filePaths As New List(Of String)()   
        filePaths.Add("c:\cache\example.txt")   
  
        policy.ChangeMonitors.Add(New HostFileChangeMonitor(filePaths))   
  
        ' Fetch the file contents.  
        fileContents = File.ReadAllText("c:\cache\example.txt")   
  
        cache.[Set]("filecontents", fileContents, policy)   
    End If   
  
    Label1.Text = fileContents  
End Sub  
private void btnGet_Click(object sender, EventArgs e)  
{  
    ObjectCache cache = MemoryCache.Default;  
    string fileContents = cache["filecontents"] as string;  
  
    if (fileContents == null)  
    {  
        CacheItemPolicy policy = new CacheItemPolicy();  
  
        List<string> filePaths = new List<string>();  
        filePaths.Add("c:\\cache\\example.txt");  
  
        policy.ChangeMonitors.Add(new   
        HostFileChangeMonitor(filePaths));  
  
        // Fetch the file contents.  
        fileContents =   
            File.ReadAllText("c:\\cache\\example.txt");  
  
        cache.Set("filecontents", fileContents, policy);  
    }  
  
    Label1.Text = fileContents;  
}  

備註

MemoryCache 類別是抽象 ObjectCache 類別的具體實作。The MemoryCache class is a concrete implementation of the abstract ObjectCache class.

注意

MemoryCache 類別類似于 ASP.NET Cache 類別。The MemoryCache class is similar to the ASP.NET Cache class. 如果您已使用 ASP.NET Cache 類別,MemoryCache 類別有許多屬性和方法,可存取您將熟悉的快取。The MemoryCache class has many properties and methods for accessing the cache that will be familiar to you if you have used the ASP.NET Cache class. CacheMemoryCache 類別之間的主要差異,在於 MemoryCache 類別已變更為可供 .NET Framework.NET Framework 不是 ASP.NET 應用程式的應用程式使用。The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed to make it usable by .NET Framework.NET Framework applications that are not ASP.NET applications. 例如,MemoryCache 類別沒有 System.Web 元件的相依性。For example, the MemoryCache class has no dependencies on the System.Web assembly. 另一個差異是,您可以建立 MemoryCache 類別的多個實例,以便在相同的應用程式和相同的 AppDomain 實例中使用。Another difference is that you can create multiple instances of the MemoryCache class for use in the same application and in the same AppDomain instance.

MemoryCache 類別不允許 null 做為快取中的值。The MemoryCache class does not allow null as a value in the cache. 嘗試新增或變更值為 null 的快取專案將會失敗。Any attempt to add or change a cache entry with a value of null will fail.

MemoryCache 類型不會執行快取區域The MemoryCache type does not implement cache regions. 因此,當您呼叫的 MemoryCache 方法會執行包含區域參數的基底方法時,請勿傳遞參數的值。Therefore, when you call MemoryCache methods that implement base methods that contain a parameter for regions, do not pass a value for the parameter. 使用區域參數的方法全都會提供預設的 null 值。The methods that use the region parameter all supply a default null value. 例如,MemoryCache.AddOrGetExisting 方法多載具有 regionName 參數,其預設值為 nullFor example, the MemoryCache.AddOrGetExisting method overload has a regionName parameter whose default value is null.

建構函式

MemoryCache(String, NameValueCollection)

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

MemoryCache(String, NameValueCollection, Boolean)

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

屬性

CacheMemoryLimit

取得電腦上可供快取使用的記憶體數量 (以位元組為單位)。Gets the amount of memory on the computer, in bytes, that can be used by the cache.

Default

取得預設 MemoryCache 執行個體的參考。Gets a reference to the default MemoryCache instance.

DefaultCacheCapabilities

取得快取提供之功能的說明。Gets a description of the features that the cache provides.

Item[String]

使用 MemoryCache 類別之執行個體的預設索引子屬性,取得或設定快取中的值。Gets or sets a value in the cache by using the default indexer property for an instance of the MemoryCache class.

Name

取得快取的名稱。Gets the name of the cache.

PhysicalMemoryLimit

取得快取可使用的實體記憶體百分比。Gets the percentage of physical memory that the cache can use.

PollingInterval

取得快取更新其記憶體統計資料之前的最多所需時間。Gets the maximum time after which the cache updates its memory statistics.

方法

Add(CacheItem, CacheItemPolicy)

將快取項目插入快取中做為 CacheItem 執行個體,並加入有關如何收回項目的詳細資料。Inserts a cache entry into the cache as a CacheItem instance, and adds details about how the entry should be evicted.

Add(String, Object, CacheItemPolicy, String)

在衍生類別中遭覆寫時,在快取中插入快取項目,並指定關於要如何收回項目的資訊。When overridden in a derived class, inserts a cache entry into the cache, specifying information about how the entry will be evicted.

(繼承來源 ObjectCache)
Add(String, Object, DateTimeOffset, String)

在衍生類別中覆寫時,在快取中插入快取項目,但不覆寫任何現有的快取項目。When overridden in a derived class, inserts a cache entry into the cache without overwriting any existing cache entry.

(繼承來源 ObjectCache)
AddOrGetExisting(CacheItem, CacheItemPolicy)

使用指定的 CacheItem 執行個體和有關如何收回項目的詳細資料,在快取中加入快取項目。Adds a cache entry into the cache using the specified CacheItem instance and details about how to evict the entry.

AddOrGetExisting(String, Object, CacheItemPolicy, String)

使用指定的索引鍵、值和有關如何收回快取項目的指定詳細資料,在快取區中插入快取項目。Inserts a cache entry into the cache using the specified key and value and the specified details for how it is to be evicted.

AddOrGetExisting(String, Object, DateTimeOffset, String)

使用指定索引鍵、值和絕對逾期值,在快取區中插入快取項目。Adds a cache entry into the cache using the specified key and a value and an absolute expiration value.

Contains(String, String)

判斷快取項目是否存在於快取中。Determines whether a cache entry exists in the cache.

CreateCacheEntryChangeMonitor(IEnumerable<String>, String)

建立 CacheEntryChangeMonitor 物件,該物件可觸發事件以回應指定快取項目的變更。Creates a CacheEntryChangeMonitor object that can trigger events in response to changes to specified cache entries.

Dispose()

MemoryCache 類別的目前執行個體所使用的資源全部釋出。Releases all resources that are used by the current instance of the MemoryCache class.

Equals(Object)

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

(繼承來源 Object)
Get(String, String)

從快取傳回項目。Returns an entry from the cache.

GetCacheItem(String, String)

CacheItem 執行個體形式從快取傳回指定的項目。Returns the specified entry from the cache as a CacheItem instance.

GetCount(String)

傳回快取中的快取項目總數。Returns the total number of cache entries in the cache.

GetEnumerator()

建立列舉程式,此列舉程式可用來逐一查看快取項目的集合。Creates an enumerator that can be used to iterate through a collection of cache entries.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLastSize(String)

取得具名區域的大小。Gets the size of the named region.

GetType()

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

(繼承來源 Object)
GetValues(IEnumerable<String>, String)

傳回對應至指定之索引鍵的一組快取項目。Returns a set of cache entries that correspond to the specified keys.

GetValues(String, String[])

取得對應至指定索引鍵的快取項目組。Gets a set of cache entries that correspond to the specified keys.

(繼承來源 ObjectCache)
MemberwiseClone()

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

(繼承來源 Object)
Remove(String, CacheEntryRemovedReason, String)

使用原因,從快取中移除快取項目。Removes a cache entry from the cache using the reason.

Remove(String, String)

從快取中移除一個快取項目。Removes a cache entry from the cache.

Set(CacheItem, CacheItemPolicy)

使用 CacheItem 執行個體提供快取項目所需的索引鍵和值,以在快取中插入快取項目。Inserts a cache entry into the cache by using a CacheItem instance to supply the key and value for the cache entry.

Set(String, Object, CacheItemPolicy, String)

使用索引鍵、值和清除,在快取區中插入快取項目。Inserts a cache entry into the cache by using a key and a value and eviction.

Set(String, Object, DateTimeOffset, String)

使用索引鍵、值並指定以時間為基礎的到期詳細資訊,在快取區中插入快取項目。Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.

ToString()

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

(繼承來源 Object)
Trim(Int32)

從快取物件移除指定百分比的快取項目。Removes a specified percentage of cache entries from the cache object.

明確介面實作

IEnumerable.GetEnumerator()

提供在泛型集合上執行反覆項目的支援。Provides support for iteration over a generic collection.

IEnumerable<KeyValuePair<String,Object>>.GetEnumerator()

支援在強類型集合上反覆運算。Supports iteration over a strongly typed collection.

(繼承來源 ObjectCache)

擴充方法

CopyToDataTable<T>(IEnumerable<T>)

根據輸入 DataTable 物件 (其中泛型參數 DataRowIEnumerable<T>) 傳回包含 T 物件複本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

根據輸入 DataRow 物件 (其中泛型參數 DataTableIEnumerable<T>),將 T 物件複製到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

根據輸入 DataRow 物件 (其中泛型參數 DataTableIEnumerable<T>),將 T 物件複製到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

傳回包含來源集合中每個節點祖系的項目集合。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

傳回包含來源集合中每個節點祖系的已篩選項目集合。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

傳回來源集合中每個文件和項目之子代節點的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

傳回來源集合中每個項目和文件的子項目集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

傳回來源集合中每個項目和文件的已篩選子項目集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

傳回包含來源集合中所有節點的節點集合,依據文件順序來排序。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

傳回來源集合中每個文件和項目的子節點集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

在來源集合中,從每一個節點的父節點移除這些節點。Removes every node in the source collection from its parent node.

適用於

執行緒安全性

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

另請參閱