Get Started with a Windows Server AppFabric Cache Client (XML)

Windows Server AppFabric offers the option to configure the cache client programmatically or with an application configuration file. The procedures in this topic describe how to configure a cache client for your application by using an XML-based application configuration file. For information about how to do this programmatically, see Get Started with a Windows Server AppFabric Cache Client.

For more information about the application configuration settings, see Application Configuration Settings (Windows Server AppFabric Caching).

These procedures assume that you have already prepared your development environment, set references to the AppFabric caching assemblies, and so on. For more information, see Preparing the Cache Client Development Environment (Windows Server AppFabric Caching).

To configure a cache client using an application configuration file

  1. From the Project menu in Visual Studio, select Add New Item.

  2. Select Application Configuration File, name the file App.config, and then click Add.

  3. Paste the XML example in the following section inside the <configuration> tags of your App.config file. Your application may use the application configuration file for other purposes, but make sure that the configSections element remains the first element under the configuration tag.

  4. Update or add host elements for the cache hosts as appropriate for your environment. For each:

    • Use the name attribute to specify the computer name of the cache host.

    • Use the cachePort attribute to specify the cache port number of the host.

  5. In your code, create a DataCacheFactory object using the default construct. By not passing configuration parameters to the DataCacheFactory object, your application will use the configuration settings in the App.config file.

  6. To begin using the cache client, use the GetCache method to create a DataCache object.

Example

This example application configuration file is configured to point to two servers, CacheServer1 and CacheServer2. Replace the server names in this example with those of your cache servers. Add or remove host tags as necessary to suit your environment.

Ideally, specify those cache hosts that have been designated lead hosts. Lead hosts are usually the first cache servers installed in the cluster. For more information about lead hosts, see Windows Server AppFabric Caching Physical Architecture Diagram.

You can determine which hosts are lead hosts by using the Windows PowerShell administration tool. For more information about Windows PowerShell, see Using Windows PowerShell to Manage Windows Server AppFabric Caching Features.

Note

Visual Basic may at first automatically add elements to your application configuration file. Those additional elements are not required by the caching features of AppFabric and may be deleted if you do not otherwise need them for your application.

<?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>

After you have specified the cache client configuration settings in the application configuration file, begin programming your cache-enabled application. This example creates a DataCacheFactory object named CacheFactory1 using the default constructor. Because the cache client configuration settings are not passed to the first parameter of the DataCacheFactory constructor, the cache client will be configured based on the settings specified in the application configuration file.

Note

For performance reasons, we recommend that you minimize the number of DataCacheFactory objects created in a cache-enabled application. Store the DataCacheFactory object in a variable available to all parts of the application that use cache clients.

Next, the GetCache method is used to create a DataCache object, named myCache1. Then the Add method is called to add an object to cache.

' 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");

See Also

Concepts

Get Started with a Windows Server AppFabric Cache Client (XML)
Enable Windows Server AppFabric Local Cache (XML)
Configuring an ASP.NET Session State Provider (Windows Server AppFabric Caching)
Cache Clients and Local Cache (Windows Server AppFabric Caching)
Using Configuration Methods (Windows Server AppFabric Caching)
Windows Server AppFabric Caching Concepts
Developing a Cache Client (Windows Server AppFabric Caching)