Windows Server AppFabric 缓存客户端 (XML) 入门

Windows Server AppFabric 提供以编程方式或使用应用程序配置文件配置缓存客户端的选项。 本主题中的过程介绍如何使用基于 XML 的应用程序配置文件配置应用程序的缓存客户端。 有关如何以编程方式执行此操作的信息,请参阅Windows Server AppFabric 缓存客户端入门

有关应用程序配置设置的详细信息,请参阅应用程序配置设置(Windows Server AppFabric 缓存)

这些过程假定您已经准备好了开发环境,设置了对 AppFabric 缓存程序集的引用等等。 有关详细信息,请参阅准备缓存客户端开发环境(Windows Server AppFabric 缓存)

使用应用程序配置文件配置缓存客户端的步骤

  1. 从 Visual Studio 的“项目”菜单中选择“添加新项”。

  2. 选择“应用程序配置文件”,将该文件命名为 App.config,然后单击“添加”。

  3. 将下一节中的 XML 示例粘贴到 App.config 文件的 <configuration> 标记内。 应用程序可能会将应用程序配置文件用于其他用途,但请确保 configSections 元素仍为 configuration 标记下的第一个元素。

  4. 为缓存主机更新或添加适合您环境的 host 元素。 为每个元素执行以下操作:

    • 使用 name 属性指定缓存主机的计算机名称。

    • 使用 cachePort 属性指定主机的缓存端口号。

  5. 在您的代码中,使用默认构造创建 DataCacheFactory 对象。 通过不向 DataCacheFactory 对象传递配置参数,应用程序将使用 App.config 文件中的配置设置。

  6. 要开始使用该缓存客户端,请使用 GetCache 方法创建 DataCache 对象。

示例

本示例应用程序配置文件配置为指向两个服务器(CacheServer1CacheServer2)。 将本示例中的服务器名称替换为您的缓存服务器名称。 根据需要添加或删除主机标记,以符合您的环境。

理想情况下,请指定那些已指定为主要主机的缓存主机。 主要主机通常是安装在群集中的首个缓存服务器。 有关主要主机的详细信息,请参阅 Windows Server AppFabric 缓存物理体系结构示意图

您可以使用 Windows PowerShell 管理工具确定哪些主机是主要主机。 有关 Windows PowerShell 的详细信息,请参阅 使用 Windows PowerShell 管理 Windows Server AppFabric 缓存功能

备注

Visual Basic 可能会首先自动将元素添加到应用程序配置文件中。 AppFabric 的缓存功能不需要这些额外元素,如果应用程序的其他功能不需要这些元素,则可以将其删除。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <!--configSections must be the FIRST element -->
   <configSections>
      <!-- required to read the <dataCacheClient> element -->
      <section name="dataCacheClient"
         type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
            Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, 
            Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         allowLocation="true"
         allowDefinition="Everywhere"/>
   </configSections>

   <dataCacheClient>
      <hosts>
         <host
            name="CacheServer1"
            cachePort="22233"/>
         <host
            name="CacheServer2"
            cachePort="22233"/>
      </hosts>
   </dataCacheClient>
</configuration>

在应用程序配置文件中指定了缓存客户端配置设置后,即可开始对已启用缓存的应用程序进行编程。 本示例使用默认构造函数创建了一个名为 CacheFactory1DataCacheFactory 对象。 因为未将缓存客户端配置设置传递给 DataCacheFactory 构造函数的第一个参数,所以缓存客户端将基于应用程序配置文件中指定的设置进行配置。

备注

出于性能方面的原因,我们建议您最大限度地减少在已启用缓存的应用程序中创建的 DataCacheFactory 对象的数量。 将 DataCacheFactory 对象存储到使用缓存客户端的应用程序的所有部件都可以使用的变量中。

接下来,使用 GetCache 方法创建一个名为 myCache1DataCache 对象。 然后调用 Add 方法将对象添加到缓存。

' Use configuration from the application configuration file.
Dim CacheFactory1 As DataCacheFactory = New DataCacheFactory()

' Get cache client for cache "NamedCache1".
Dim myCache1 As DataCache = CacheFactory1.GetCache("NamedCache1")

' Add an object to the cache.
myCache1.Add("helloKey", "hello world")
// Use configuration from the application configuration file.
DataCacheFactory CacheFactory1 = new DataCacheFactory();

// Get cache client for cache "NamedCache1".
DataCache myCache1 = CacheFactory1.GetCache("NamedCache1");

// Add an object to the cache.
myCache1.Add("helloKey", "hello world");

另请参阅

概念

Windows Server AppFabric 缓存客户端 (XML) 入门
启用 Windows Server AppFabric 本地缓存 (XML)
配置 ASP.NET 会话状态提供程序(Windows Server AppFabric 缓存)
缓存客户端和本地缓存(Windows Server AppFabric 缓存)
使用配置方法(Windows Server AppFabric 缓存)
Windows Server AppFabric 缓存概念
开发缓存客户端(Windows Server AppFabric 缓存)

  2011-12-05