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. 缓存条目使用 @no__t 0 对象为缓存条目提供逐出和过期详细信息。The cache entry uses a CacheItemPolicy object to provide eviction and expiration details for the cache entry. 它还使用 @no__t 0 对象来监视文件系统中的源数据(即文件)的状态。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.

备注

@No__t-0 类类似于 ASP.NET @no__t 的类。The MemoryCache class is similar to the ASP.NET Cache class. 如果使用了 ASP.NET Cache 类,则 @no__t 的类具有许多用于访问缓存的属性和方法。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. @No__t-0 类与 @no__t 1 类之间的主要区别在于,MemoryCache 类已更改,使其可供不是 ASP.NET 应用程序的 @no__t 3 应用程序使用。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. 例如,@no__t 0 类对 @no__t 的程序集没有依赖项。For example, the MemoryCache class has no dependencies on the System.Web assembly. 另一差别在于,您可以创建 MemoryCache 类的多个实例,以便在同一个应用程序和同一个 @no__t 实例中使用。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.

@No__t-0 类不允许 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.

@No__t 0 类型不实现缓存区域The MemoryCache type does not implement cache regions. 因此,在调用实现包含区域参数的基方法 @no__t 0 方法时,不要传递参数的值。Therefore, when you call MemoryCache methods that implement base methods that contain a parameter for regions, do not pass a value for the parameter. 使用 region 参数的方法都提供默认 @no__t 0 值。The methods that use the region parameter all supply a default null value. 例如,@no__t 0 方法重载具有一个 @no__t 参数,其默认值为 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()

返回一个表示当前对象的 string。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>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<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.

另请参阅