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 的浅表副本。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.

另请参阅