<memoryCache > 元素(缓存设置)<memoryCache> Element (Cache Settings)

定义一个用于配置基于 MemoryCache 类的缓存的元素。Defines an element that is used to configure a cache that is based on the MemoryCache class. MemoryCacheElement 类定义可以用于配置缓存的 memoryCache 元素。The MemoryCacheElement class defines a memoryCache element that you can use to configure the cache. 可以在单个应用程序中使用 MemoryCache 类的多个实例。Multiple instances of the MemoryCache class can be used in a single application. 配置文件中的每个 memoryCache 元素可以包含一个命名 MemoryCache 实例的设置。Each memoryCache element in the configuration file can contain settings for a named MemoryCache instance.

<configuration> <configuration>
   <> 缓存  <system.runtime.caching>
     <memoryCache >    <memoryCache>

语法Syntax

<memoryCache>   
    <namedCaches>  
        <!-- child elements -->  
    </namedCaches>   
</memoryCache>  

类型Type

MemoryCache 类。MemoryCache class.

特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

特性Attribute 描述Description
CacheMemoryLimitMegabytes MemoryCache 对象的实例可以增长到的最大内存大小(以兆字节为单位)。The maximum memory size, in megabytes, that an instance of a MemoryCache object can grow to. 默认值为 0,这意味着默认情况下使用 MemoryCache 类的自动调整大小启发。The default value is 0, which means that the MemoryCache class's autosize heuristics are used by default.
Name 缓存配置的名称。The name of the cache configuration.
PhysicalMemoryLimitPercentage 缓存可以使用的物理内存的百分比。The percentage of physical memory that can be used by the cache. 默认值为 0,这意味着默认情况下使用 MemoryCache 类的自动调整大小启发。The default value is 0, which means that the MemoryCache class's autosize heuristics are used by default.
PollingInterval 一个时间间隔的值,在该时间间隔之后,缓存实现会将当前内存负载与为缓存实例设置的基于绝对值和百分比的内存限制进行比较。A value that indicates the time interval after which the cache implementation compares the current memory load against the absolute and percentage-based memory limits that are set for the cache instance. 该值以“HH:MM:SS”格式输入。The value is entered in "HH:MM:SS" format.

子元素Child Elements

元素Element 描述Description
<namedCaches><namedCaches> 包含 namedCache 实例的配置设置的集合。Contains a collection of configuration settings for the namedCache instance.

父元素Parent Elements

元素Element 描述Description
<configuration><configuration> 指定公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。Specifies the root element in every configuration file that is used by the common language runtime and .NET Framework applications.
<system.runtime.caching><system.runtime.caching> 包含使你可以在 .NET Framework 中内置的应用程序中实现输出缓存的类型。Contains types that let you implement output caching in applications that are built into the .NET Framework.

备注Remarks

MemoryCache 类是抽象 ObjectCache 类的具体实现。The MemoryCache class is a concrete implementation of the abstract ObjectCache class. MemoryCache 类的实例可以随来自应用程序配置文件的配置信息一起提供。Instances of the MemoryCache class can be supplied with configuration information from application configuration files. memoryCache 配置节包含 namedCaches 配置集合。The memoryCache configuration section contains a namedCaches configuration collection.

基于内存的缓存对象进行初始化时,它首先尝试查找与传递给内存缓存构造函数的参数中的名称进行匹配的 namedCaches 项。When a memory-based cache object is initialized, it first tries to find a namedCaches entry that matches the name in the parameter that is passed to the memory cache constructor. 如果找到 namedCaches 项,则从配置文件检索轮询和内存管理信息。If a namedCaches entry is found, the polling and memory-management information are retrieved from the configuration file.

初始化过程随后使用构造函数中配置信息的名称/值对的可选集合来确定是否重写了任何配置项。The initialization process then determines whether any configuration entries were overridden, by using the optional collection of name/value pairs of configuration information in the constructor. 如果在名称/值对集合中传递以下值之一,则这些值会重写从配置文件获取的信息:If you pass any one of the following values in the name/value pair collection, these values override information obtained from the configuration file:

示例Example

下面的示例演示如何通过MemoryCache name将属性设置为 "default",将对象的名称设置为默认缓存对象名称。The following example shows how to set the name of the MemoryCache object to the default cache object name by setting the name attribute to "Default".

cacheMemoryLimitMegabytes 属性和 physicalMemoryLimitPercentage 属性设置为零。The cacheMemoryLimitMegabytes attribute and the physicalMemoryLimitPercentage attribute are set to zero. 将这些特性设置为零意味着默认情况下使用 MemoryCache 自动调整大小试探法。Setting these attributes to zero means that the MemoryCache autosizing heuristics are used by default. 每隔两分钟,缓存实现应对当前内存负载和基于百分比的绝对内存限制进行比较。The cache implementation should compare the current memory load against the absolute and percentage-based memory limits every two minutes.

<configuration>  
  <system.runtime.caching>  
    <memoryCache>  
      <namedCaches>  
          <add name="Default"   
               cacheMemoryLimitMegabytes="0"   
               physicalMemoryLimitPercentage="0"  
               pollingInterval="00:02:00" />  
      </namedCaches>  
    </memoryCache>  
  </system.runtime.caching>  
</configuration>  

请参阅See also