Get Started with a Windows Server AppFabric Cache Client
Windows Server AppFabric 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 Windows Server AppFabric 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.
Tip
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.
Note
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 (Windows Server AppFabric 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
Servers
property of the DataCacheFactoryConfiguration object.Pass the DataCacheFactoryConfiguration object to the constructor of the DataCacheFactory class.
Use the GetDefaultCache method or the GetCache method to obtain a DataCache class based on the settings of the DataCacheFactoryConfiguration object.
Example
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 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.
First the servers
array is created. This example configures a cache host that is named CacheServer2
.
' Declare array for cache host(s).
Dim servers(0) As DataCacheServerEndpoint
servers(0) = New DataCacheServerEndpoint("CacheServer2", 22233)
// Declare array for cache host(s).
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint("CacheServer2", 22233);
Next create a DataCacheFactoryConfiguration object. Assign the servers
array to the Servers
property.
' Setup the DataCacheFactory configuration.
Dim factoryConfig As DataCacheFactoryConfiguration
factoryConfig = New DataCacheFactoryConfiguration
factoryConfig.Servers = servers
// 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 NamedCache1
.
' Create a configured DataCacheFactory object.
Dim mycacheFactory As DataCacheFactory
mycacheFactory = New DataCacheFactory(factoryConfig)
' Get a cache client for the cache "NamedCache1".
Dim myDefaultCache As DataCache
myDefaultCache = mycacheFactory.GetCache("NamedCache1")
// Create a configured DataCacheFactory object.
DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig);
// Get a cache client for the cache "NamedCache1".
DataCache myDefaultCache = mycacheFactory.GetCache("NamedCache1");
See Also
Concepts
Enable Windows Server AppFabric Local Cache
Change the Cache Client Logging Level (Windows Server AppFabric Caching)
Cache Clients and Local Cache (Windows Server AppFabric Caching)
Windows Server AppFabric Caching Concepts
Developing a Cache Client (Windows Server AppFabric Caching)