Azure 托管缓存服务的缓存客户端的配置设置
重要
Microsoft 建议所有新开发使用 Azure Redis 缓存。 有关选择 Azure 缓存产品/服务的当前文档和指南,请参阅 哪种 Azure 缓存产品/服务适合我?
本主题介绍在.NET Framework web.config或app.config配置文件中支持托管缓存服务的要求。 它还介绍可供缓存客户端使用的配置选项。
提示
将NuGet包用于托管缓存服务时,包会自动修改配置文件。 但是,可以使用本主题进一步自定义NuGet插入的缓存客户端设置。 有关Caching NuGet包的详细信息,请参阅使用 Caching NuGet 包配置缓存客户端。
本主题涵盖以下节。
<configSections>
<dataCacheClients>
<dataCacheClient>
<autoDiscover>
<localCache>
<clientNotification>
<serializationProperties>
<securityProperties>
<cacheDiagnostics>
- <crashDump>
缓存客户端配置示例
以下示例显示了与托管缓存服务相关的web.config或app.config文件的各部分。 在本例中,default
缓存客户端将访问 mycache.cache.windows.net
上托管的缓存,并且本地缓存已启用。
<configuration>
<configSections>
<section name="dataCacheClients"
type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection,
Microsoft.ApplicationServer.Caching.Core"
allowLocation="true"
allowDefinition="Everywhere" />
<section name="cacheDiagnostics"
type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection,
Microsoft.ApplicationServer.Caching.AzureCommon"
allowLocation="true"
allowDefinition="Everywhere" />
</configSections>
<dataCacheClients>
<dataCacheClient name="default">
<!--To use the in-role flavor of Azure Caching,
set identifier to be the cache cluster role name -->
<!--To use the Azure Caching Service,
set identifier to be the endpoint of the cache cluster -->
<autoDiscover isEnabled="true" identifier="mycache.cache.windows.net" />
<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />
<!--Use this section to specify security settings for connecting to your cache.
This section is not required if your cache is hosted on a role that is a part
of your cloud service. -->
<securityProperties mode="Message" sslEnabled="false">
<messageSecurity authorizationInfo="MyAuthorizationKey" />
</securityProperties>
</dataCacheClient>
</dataCacheClients>
<cacheDiagnostics>
<crashDump dumpLevel="Off" dumpStorageQuotaInMB="100" />
</cacheDiagnostics>
</configuration>
configSections
以下两个命名 节 元素应添加到 configSections 元素中的节列表。
名称 | 说明 |
---|---|
dataCacheClients |
定义配置文件的 dataCacheClients 元素。 此节为所有缓存客户端配置指定设置。 |
cacheDiagnostics |
定义配置文件的 cacheDiagnostics 元素。 本部分配置特定于托管缓存服务的诊断。 |
以下示例演示如何定义这些节。
<section name="dataCacheClients"
type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection,
Microsoft.ApplicationServer.Caching.Core"
allowLocation="true"
allowDefinition="Everywhere" />
<section name="cacheDiagnostics"
type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection,
Microsoft.ApplicationServer.Caching.AzureCommon"
allowLocation="true"
allowDefinition="Everywhere" />
dataCacheClients
每个 dataCacheClients 元素都包含一个或多个命名 的 dataCacheClient 元素。 dataCacheClients 元素没有任何属性。 如果能够有多个缓存客户端配置节,则应用程序可以对不同类型的缓存数据使用不同的设置。
dataCacheClient
每个 dataCacheClients 元素都包含一个或多个命名 的 dataCacheClient 元素。 应用程序代码通过引用关联的 dataCacheClient 节的名称来加载缓存客户端的设置。
dataCacheClients 元素没有任何属性。 但是子 dataCacheClient 元素具有多个可用的属性。 下表对这些属性进行了介绍。
属性 | 说明 |
---|---|
名称 |
缓存客户端配置的名称。 |
isCompressionEnabled |
启用或禁用压缩。 可能的值包括 |
useConnectionPool |
启用或禁用连接池。 可能的值包括 |
maxConnectionsToServer |
当 useConnectionPool 设置为 |
以下各节介绍了每个 dataCacheClient 元素中的可用子元素。
autoDiscover
autoDiscover 元素可帮助缓存客户端自动连接到目标缓存。
下表描述了 自动发现 元素的可用属性。
属性 | 说明 |
---|---|
isEnabled |
|
identifier |
对于托管缓存服务,它是缓存终结点的名称。 对于In-Role缓存,它是托管In-Role缓存的同一云服务部署中的角色的名称。 |
<autoDiscover isEnabled="true" identifier="mycache.cache.windows.net" />
localCache
localCache 元素控制本地缓存的使用。 默认情况下,不启用本地缓存。 启用时,从缓存中检索的项目将本地存储在客户端计算机的内存中。 这将改善后续的 get 请求的性能,但它可能导致本地缓存的版本与缓存中的实际项目之间存在数据不一致的情况。 有关详细信息,请参阅 Azure 本地缓存托管缓存服务。
下表描述了 localCache 元素的可用属性。
属性 | 说明 |
---|---|
isEnabled |
设置为 |
同步 |
确定使本地缓存失效的方式。 可能值为 |
objectCount |
要在本地缓存中存储的对象的最大数目。 默认值为 10000。 |
ttlValue |
本地缓存中保留对象的秒数。 默认值为 300 秒。 |
注意
标准版和高级版缓存产品支持通知,必须配置这些产品中的命名缓存以启用通知。 有关详细信息,请参阅 Azure 托管缓存服务通知。
<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />
clientNotification
下表描述了 clientNotification 元素的可用属性。 此元素负责控制缓存客户端的通知行为。
属性 | 说明 |
---|---|
pollInterval |
轮询尝试之间的秒数。 轮询用于检查缓存上的通知。 默认值为 |
maxQueueLength |
轮询尝试之间排队等候此缓存客户端处理的通知的最大数目。 默认值为 |
<clientNotification pollInterval="60" maxQueueLength="10000"/>
serializationProperties
下表描述了 serializationProperties 元素的可用属性。 此元素支持选择缓存项的内置序列化或自定义序列化。 有关详细信息,请参阅 Azure 托管缓存服务的序列化。
属性 | 说明 |
---|---|
serializer |
确定缓存项的序列化类型。 可能值为 |
securityProperties
下表描述了 securityProperties 元素的可用属性。
属性 | 说明 |
---|---|
mode |
可能的值包括 |
sslEnabled |
可能值为 |
securityProperties 元素具有子元素 messageSecurity。 下表描述了 messageSecurity 元素的可用属性。
属性 | 说明 |
---|---|
authorizationInfo |
缓存的访问密钥,可从中的缓存仪表板检索。 如果配置不当,缓存客户端将无法与缓存建立通信。 |
<securityProperties mode="Message" sslEnabled="false">
<messageSecurity authorizationInfo="iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmludDMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lFQ2FFPSZodJRzOi8xS8JxdGJEb281bzxudGF0az9uxGxzdGluZi5jiz8oZS5pb8QzL8dpbmRxd3MQaz50Lm5ldA==" />
</securityProperties>
cacheDiagnostics
本部分配置托管缓存服务的一些诊断设置。
警告
在托管缓存服务版本中,本主题中所述的 cacheDiagnostics 和相关部分仅适用于基于角色的云服务缓存客户端。 如果你的缓存客户端不是云服务缓存客户端,则缓存 NuGet 程序包不会添加这些部分。 有关详细信息,请参阅关于 Azure 托管缓存服务的 ClientDiagnosticLevel。
以下各节介绍每个 cacheDiagnostics 元素中的可用子元素。
crashDump
crashDump 元素控制为此应用程序收集的故障转储的类型。 托管缓存服务故障转储主要用于诊断缓存计算机的问题。 因此,客户端配置文件应关闭故障转储生成。 可以通过将 dumpLevel 属性设置为 Off
/> 来完成此操作。
启用了故障转储时,应在 ServiceDefinition.csdef 中调整缓存本地存储和诊断存储,使该大小始终大于故障存储配额和 CacheSettings.xml 中配置的日志存储配额(默认值为 1000 MB)的总和。
下表描述了 crashDump 元素的可用属性。
属性 | 说明 |
---|---|
dumpLevel |
可能的值包括 |
dumpStorageQuotaInMB |
故障转储大小的上限。 |
scheduledTransferPeriodInMinutes |
从故障转储到诊断存储的传输之间的分钟数。 |
<crashDump dumpLevel="Off" dumpStorageQuotaInMB="100" scheduledTransferPeriodInMinutes="5" />