How to: Get Started with a Routing Client (Code) (Velocity)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Microsoft project code named "Velocity" offers the option to configure the cache client programmatically or with an application configuration file. The following procedures describe how to programmatically configure a simple client for your application. For information about how to do this with an application configuration file, see How to: Get Started with a Routing Client (XML) (Velocity).

When you programmatically configure your cache client, the configuration settings are passed to the DataCacheFactory class constructor.

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.

The cache client type is defined by the routingClient parameter in the DataCacheFactory class constructor. For a routing client, the routingClient parameter must be true. For more information about the application configuration settings, see Application Configuration Settings (Velocity).

Note

These procedures assume that you have already prepared your development environment and set references to the "Velocity" assemblies, and so on. For more information, see How to: Prepare the Development Environment (Velocity)

To configure a routing client programmatically

  1. Create an array of DataCacheServerEndpoint objects to specify the cache hosts for the client.

  2. Configure your cache hosts by assigning the cache host array from the previous step to the servers parameter of the DataCacheFactory constructor.

  3. Select a routing client type by assigning a true value to the routingClient parameter of the DataCacheFactory constructor.

  4. Configure local cache by assigning a true or false value to the localCache parameter of the DataCacheFactory constructor. Use the true value to enable local cache, or a false value to disable local cache.

  5. Use the GetCache method to obtain an instance of the routing client.

Example

This example shows the programmatic configuration of a routing client for a cache called NamedCache1. This routing 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 the Physical Model section in General Concept Models (Velocity). You can determine which hosts are lead hosts by using the PowerShell administration tool. For more information about PowerShell, see Cache Administration with PowerShell (Velocity).

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

'specify cache host(s)
servers(0) = New DataCacheServerEndpoint("CacheServer2", _
                            22233, "DistributedCacheService")
//declare array for cache host(s)
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];

//specify cache host(s)
servers[0] = new DataCacheServerEndpoint("CacheServer2", 
                        22233, "DistributedCacheService");

Next, pass the configuration parameters to the DataCacheFactory class constructor and instantiate the cache client that has the GetCache method. This example creates a cache client for a cache that is named NamedCache1.

'configure routing client with local cache disabled
Dim myRoutingCacheFactory As DataCacheFactory _
    = New DataCacheFactory(servers, True, False)

'get cache client for cache "NamedCache1"
Dim myRoutingCacheClient As DataCache _
    = myRoutingCacheFactory.GetCache("NamedCache1")
//configure routing client with local cache disabled
DataCacheFactory myRoutingCacheFactory 
    = new DataCacheFactory(servers, true, false);

//get cache client for cache "NamedCache1"
DataCache myRoutingCacheClient 
    = myRoutingCacheFactory.GetCache("NamedCache1");

See Also

Tasks

How to: Get Started with a Simple Client (Code) (Velocity)
How to: Enable Local Cache (Code) (Velocity)
How to: Set Log Sink Levels (Code) (Velocity)

Concepts

Cache Clients and Local Cache (Velocity)

Other Resources

Configuring the Cache Client with XML (Velocity)
Cache Concepts (Velocity)
Programming Guide (Velocity)