Share via


Get Started with a Simple Client

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 Get Started with a Simple Client (XML).

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 simple client, the routingClient parameter must be false. For more information about the application configuration settings, see Application Configuration Settings.

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 Prepare the Development Environment.

To configure a simple 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 simple client type by assigning a false 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 simple client for a cache called NamedCache1. This simple client has local cache disabled, and is configured to point to a cache server that is named CacheServer2. 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 as lead hosts. Lead hosts are usually the first cache servers installed in the cluster. For more information about lead hosts, see Velocity Physical Architecture Diagram. You can determine which hosts are lead hosts by using the PowerShell administration tool. For more information about PowerShell, see Cache Administration with Windows PowerShell.

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 simple client with local cache disabled
Dim mySimpleCacheFactory As DataCacheFactory _
    = New DataCacheFactory(servers, False, False)

'get cache client for cache "NamedCache1"
Dim mySimpleCacheClient As DataCache _
    = mySimpleCacheFactory.GetCache("NamedCache1")
//configure simple client with local cache disabled
DataCacheFactory mySimpleCacheFactory 
    = new DataCacheFactory(servers, false, false);

//get cache client for cache "NamedCache1"
DataCache mySimpleCacheClient 
    = mySimpleCacheFactory.GetCache("NamedCache1");

See Also

Concepts

Get Started with a Routing Client
Enable Local Cache
Set Log Sink Levels
Cache Clients and Local Cache
Configuring the Cache Client with XML
Cache Concepts (Velocity)
Developing for Cache Server