Get Started with a Cache Client
Microsoft AppFabric 1.1 for Windows Server offers the option to configure the cache client programmatically or with an application configuration file. The following procedures describe how to programmatically configure a cache client used by your application. For information about how to do this with an application configuration file, see Get Started with a Cache Client (XML).
To programmatically configure your cache client, create an instance of the DataCacheFactoryConfiguration class to specify configuration settings. Then pass this object to the DataCacheFactory class constructor.
|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.|
|These procedures assume that you have already prepared your development environment to use the Cache Client features. For more information, see Preparing the Cache Client Development Environment (AppFabric 1.1 Caching).|
To configure a cache client programmatically
Create an array of DataCacheServerEndpoint objects to specify the cache hosts for the client.
Create an instance of the DataCacheFactoryConfiguration class.
Configure your cache hosts by assigning the cache host array from the first step to the
Serversproperty of the DataCacheFactoryConfiguration object.
Pass the DataCacheFactoryConfiguration object to the constructor of the DataCacheFactory class.
This example shows the programmatic configuration of a cache client for a cache named
NamedCache1. This cache client has local cache disabled and is configured to point to a cache server that is named
CacheServer2. To use this example in your own application, replace the server properties in this example with those of your cache server(s). Add additional DataCacheServerEndpoint objects to the
servers array for each of the other cache hosts in the cluster.
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 AppFabric Caching Physical Architecture Diagram (AppFabric 1.1 Caching). You can determine which hosts are lead hosts by using the Windows PowerShell administration tool. For more information about Windows PowerShell, see Cache Administration with Windows PowerShell (AppFabric 1.1).
servers array is created. This example configures a cache host that is named
// Declare array for cache host(s). DataCacheServerEndpoint servers = new DataCacheServerEndpoint; servers = new DataCacheServerEndpoint("CacheServer2", 22233);
Next create a DataCacheFactoryConfiguration object. Assign the
servers array to the
// Setup the DataCacheFactory configuration. DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration(); factoryConfig.Servers = servers;
Next, pass the DataCacheFactoryConfiguration object to the DataCacheFactory class constructor and instantiate the cache client with the GetCache method. This example creates a cache client for a cache that is named