MemoryCache 构造函数
定义
重载
| 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. |
MemoryCache(String, NameValueCollection)
初始化 MemoryCache 类的新实例。Initializes a new instance of the MemoryCache class.
public MemoryCache (string name, System.Collections.Specialized.NameValueCollection config = default);
new System.Runtime.Caching.MemoryCache : string * System.Collections.Specialized.NameValueCollection -> System.Runtime.Caching.MemoryCache
Public Sub New (name As String, Optional config As NameValueCollection = Nothing)
参数
- name
- String
用于查找配置信息的名称。The name to use to look up configuration information.
请注意,不要求每个名称都存在配置信息。Note It is not required for configuration information to exist for every name.
如果存在匹配的配置项,则使用该配置信息来配置 MemoryCache 实例。If a matching configuration entry exists, the configuration information is used to configure the MemoryCache instance. 如果不存在匹配的配置项,则可以通过 Name 属性访问名称,因为指定的名称与 MemoryCache 实例相关联。If a matching configuration entry does not exist, the name can be accessed through the Name property, because the specified name is associated with the MemoryCache instance. 有关内存缓存配置的信息,请参见 MemoryCacheElement。For information about memory cache configuration, see MemoryCacheElement.
- config
- NameValueCollection
用于配置缓存的配置信息的名称/值对集合。A collection of name/value pairs of configuration information to use for configuring the cache.
例外
name 为 null。name is null.
未能分析 config 参数中的名称或值。A name or value in the config parameter could not be parsed.
config 集合中的值无效。A value in the config collection is invalid.
注解
在 MemoryCache 初始化类时,它将检查可能已使用构造函数中的可选参数重写的配置项 config 。When the MemoryCache class is initialized, it checks for configuration entries that might have been overridden by using the optional config parameter in the constructor. 可以在参数中传递以下参数 config 。You can pass the following parameters in the config parameter. 所有值都可以作为整数传递。All values can be passed as integers.
调用此构造函数时,将首先从应用程序配置文件中检索配置设置。When this constructor is invoked, configuration settings are first retrieved from application configuration files. 如果应用程序配置文件中不存在任何配置项,则仅应用中提供的设置 config 。If no configuration entries exist in the application configuration file, only the settings provided in config are applied. 如果应用程序配置中存在配置条目,并且还传入了信息 config ,则中的信息会 config 重写从配置文件中读取的信息。If the configuration entries in the application configuration exist, and if information is also passed in config, the information in the config overrides the information that is read from the configuration file.
分配给缓存实例的名称值通过两种方式使用:The name value that is assigned to a cache instance is used in two ways:
当存在多个实例时,帮助您跟踪多个缓存实例。To help you keep track of multiple cache instances when more than one instance exists.
在初始化实例时引用配置文件中的设置 MemoryCache 。To reference settings in the configuration file when the MemoryCache instance is initialized.
备注
没有任何机制来强制缓存实例的唯一名称。There is no mechanism to enforce unique names for cache instances. 因此,可以有多个具有相同名称的缓存实例。Therefore, it is possible to have multiple cache instances with the same name.
注意
除非需要,否则不要创建 MemoryCache 实例。Do not create MemoryCache instances unless it is required. 如果在客户端和 Web 应用程序中创建缓存实例,则 MemoryCache 应在应用程序生命周期的早期创建实例。If you create cache instances in client and Web applications, the MemoryCache instances should be created early in the application life cycle. 必须仅创建将在应用程序中使用的缓存实例数,并将对缓存实例的引用存储在可全局访问的变量中。You must create only the number of cache instances that will be used in your application, and store references to the cache instances in variables that can be accessed globally. 例如,在 ASP.NET 应用程序中,可以将引用存储在应用程序状态中。For example, in ASP.NET applications, you can store the references in application state. 如果只在应用程序中创建一个缓存实例, Default 则在需要访问缓存时,请使用默认缓存并从属性获取对该实例的引用。If you create only a single cache instance in your application, use the default cache and get a reference to it from the Default property when you need to access the cache.
适用于
MemoryCache(String, NameValueCollection, Boolean)
初始化 MemoryCache 类的新实例。Initializes a new instance of the MemoryCache class.
public:
MemoryCache(System::String ^ name, System::Collections::Specialized::NameValueCollection ^ config, bool ignoreConfigSection);
public MemoryCache (string name, System.Collections.Specialized.NameValueCollection config, bool ignoreConfigSection);
new System.Runtime.Caching.MemoryCache : string * System.Collections.Specialized.NameValueCollection * bool -> System.Runtime.Caching.MemoryCache
Public Sub New (name As String, config As NameValueCollection, ignoreConfigSection As Boolean)
参数
- name
- String
用于查找配置信息的名称。The name to use to look up configuration information.
- config
- NameValueCollection
用于配置缓存的配置信息的名称/值对集合。A collection of name/value pairs of configuration information to use for configuring the cache.
- ignoreConfigSection
- Boolean
指示是否应忽略配置部分。Indicates whether the configuration section should be ignored.