MemoryCache Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
MemoryCache(String, NameValueCollection) |
Initializes a new instance of the MemoryCache class. |
MemoryCache(String, NameValueCollection, Boolean) |
Initializes a new instance of the MemoryCache class. |
MemoryCache(String, NameValueCollection)
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
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)
Parameters
- name
- String
The name to use to look up configuration information.
Note
It is not required for configuration information to exist for every name.
If a matching configuration entry exists, the configuration information is used to configure the MemoryCache instance. 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. 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.
Exceptions
name
is null
.
A name or value in the config
parameter could not be parsed.
A value in the config
collection is invalid.
Remarks
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. 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. If no configuration entries exist in the application configuration file, only the settings provided in config
are applied. 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.
To reference settings in the configuration file when the MemoryCache instance is initialized.
Note
There is no mechanism to enforce unique names for cache instances. Therefore, it is possible to have multiple cache instances with the same name.
Caution
Do not create MemoryCache instances unless it is required. 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. For example, in ASP.NET applications, you can store the references in application state. 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.
Applies to
MemoryCache(String, NameValueCollection, Boolean)
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
- Source:
- MemoryCache.cs
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)
Parameters
- 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.