<記憶體緩存>元素(緩存設置)<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>
  <系統.運行時.緩存>  <system.runtime.caching>
    <記憶體緩存>    <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 物件執行個體可以成長的最大記憶體大小 (MB)。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> 包含 namedCache 執行個體的組態設定集合。Contains a collection of configuration settings for the namedCache instance.

父項目Parent Elements

元素Element 描述Description
<配置><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> 包含允許您在內置於 .NET 框架中的應用程式中實現輸出緩存的類型。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

下面的示例演示如何通過將MemoryCachename屬性設置為"預設"來將物件的名稱設置為預設緩存物件名稱。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