MemoryCache MemoryCache MemoryCache MemoryCache Class

定義

表示可實作記憶體內部快取的型別。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
繼承
MemoryCacheMemoryCacheMemoryCacheMemoryCache
實作

範例

下列範例會宣告預設的記憶體快取執行個體的參考。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.NETCache類別。The MemoryCache class is similar to the ASP.NET Cache class. MemoryCache類別具有許多屬性和方法,以存取會很熟悉,如果您已使用 ASP.NET 快取Cache類別。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(String, NameValueCollection) MemoryCache(String, NameValueCollection) MemoryCache(String, NameValueCollection)

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

MemoryCache(String, NameValueCollection, Boolean) MemoryCache(String, NameValueCollection, Boolean) MemoryCache(String, NameValueCollection, Boolean) MemoryCache(String, NameValueCollection, Boolean)

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

屬性

CacheMemoryLimit CacheMemoryLimit CacheMemoryLimit CacheMemoryLimit

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

Default Default Default Default

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

DefaultCacheCapabilities DefaultCacheCapabilities DefaultCacheCapabilities DefaultCacheCapabilities

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

Item[String] Item[String] Item[String] 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 Name Name Name

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

PhysicalMemoryLimit PhysicalMemoryLimit PhysicalMemoryLimit PhysicalMemoryLimit

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

PollingInterval PollingInterval PollingInterval PollingInterval

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

方法

Add(CacheItem, CacheItemPolicy) Add(CacheItem, CacheItemPolicy) Add(CacheItem, CacheItemPolicy) 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) Add(String, Object, CacheItemPolicy, String) Add(String, Object, CacheItemPolicy, String) 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.

(Inherited from ObjectCache)
Add(String, Object, DateTimeOffset, String) Add(String, Object, DateTimeOffset, String) Add(String, Object, DateTimeOffset, String) Add(String, Object, DateTimeOffset, String)

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

(Inherited from ObjectCache)
AddOrGetExisting(CacheItem, CacheItemPolicy) AddOrGetExisting(CacheItem, CacheItemPolicy) AddOrGetExisting(CacheItem, CacheItemPolicy) 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) AddOrGetExisting(String, Object, CacheItemPolicy, String) AddOrGetExisting(String, Object, CacheItemPolicy, String) 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) AddOrGetExisting(String, Object, DateTimeOffset, String) AddOrGetExisting(String, Object, DateTimeOffset, String) 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) Contains(String, String) Contains(String, String) Contains(String, String)

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

CreateCacheEntryChangeMonitor(IEnumerable<String>, String) CreateCacheEntryChangeMonitor(IEnumerable<String>, String) CreateCacheEntryChangeMonitor(IEnumerable<String>, String) CreateCacheEntryChangeMonitor(IEnumerable<String>, String)

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

Dispose() Dispose() Dispose() Dispose()

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

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

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

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

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

GetCacheItem(String, String) GetCacheItem(String, String) GetCacheItem(String, String) GetCacheItem(String, String)

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

GetCount(String) GetCount(String) GetCount(String) GetCount(String)

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

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

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

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

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

(Inherited from Object)
GetLastSize(String) GetLastSize(String) GetLastSize(String) GetLastSize(String)

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
GetValues(IEnumerable<String>, String) GetValues(IEnumerable<String>, String) GetValues(IEnumerable<String>, String) GetValues(IEnumerable<String>, String)

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

GetValues(String, String[]) GetValues(String, String[]) GetValues(String, String[]) GetValues(String, String[])

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

(Inherited from ObjectCache)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String, CacheEntryRemovedReason, String) Remove(String, CacheEntryRemovedReason, String) Remove(String, CacheEntryRemovedReason, String) Remove(String, CacheEntryRemovedReason, String)

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

Remove(String, String) Remove(String, String) Remove(String, String) Remove(String, String)

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

Set(CacheItem, CacheItemPolicy) Set(CacheItem, CacheItemPolicy) Set(CacheItem, CacheItemPolicy) 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) Set(String, Object, CacheItemPolicy, String) Set(String, Object, CacheItemPolicy, String) 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) Set(String, Object, DateTimeOffset, String) Set(String, Object, DateTimeOffset, String) 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() ToString() ToString() ToString()

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

(Inherited from Object)
Trim(Int32) Trim(Int32) Trim(Int32) Trim(Int32)

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

明確介面實作

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

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

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

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

(Inherited from ObjectCache)

Extension Methods

CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>)

傳回DataTable,其中包含副本DataRow物件,指定輸入IEnumerable<T>物件其中泛型參數TDataRowReturns 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

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)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) 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) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) 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>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>)

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

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) 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) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) 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>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) 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) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) 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>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) 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>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>)

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

Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>)

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

適用於

執行緒安全性

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

另請參閱