How to: Enable Local Cache (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 enable local cache on your cache client programmatically. For information about how to do this with your application configuration file, see How to: Enable Local Cache (XML) (Velocity).

To programmatically enable local cache when creating your cache client, you must make sure the localCache parameter in the DataCacheFactory class constructor is equal to 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 create a cache client that has local cache enabled

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

    1. Configure your cache client type by assigning a true or false value to the routingClient parameter of the DataCacheFactory constructor. Use the true value for a routing client, or a false value for a simple client.

    2. Enable local cache by assigning a true value to the localCache parameter of the DataCacheFactory constructor.

    3. (optional) Configure your cache client for cache notifications by using the DataCacheFactory constructor with the following additional parameters.

    4. syncPolicy: Use the DataCacheLocalCacheSyncPolicy enumeration to choose how locally cached objects are invalidated. Use TimeoutBased to indicate that a time-out value should be used or NotificationBased to indicate that cache notifications will be used. For more information, see Cache Clients and Local Cache (Velocity).

    5. localCacheTimeout: Use this parameter to specify the number of seconds that an object will remain in local cache before it is invalidated. This parameter is ignored if syncPolicy is set to NotificationBased.

    6. pollInterval: Use this parameter to specify the interval of frequency, in seconds, that the cache client will check with the cache cluster for cache notifications. The default value is 300 seconds. Note: Local cache is not required for cache notifications. For more information, see Cache Notifications (Velocity).

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

Example

This example shows the programmatic configuration of a routing client that has local cache enabled. This client is configured to use a cache called NamedCache1and point to a cache server that is named CacheServer2. To use the 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 as 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 with the GetCache method. This example creates a cache client for a cache that is named NamedCache1.

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.

'configure routing client with local cache enabled
Dim myRoutingLCCacheFactory As DataCacheFactory _
    = New DataCacheFactory(servers, True, True)

'get cache client for cache "NamedCache1"
Dim myRoutingLCCacheClient As DataCache _
    = myRoutingLCCacheFactory.GetCache("NamedCache1")
//configure routing client with local cache enabled
DataCacheFactory myRoutingLCCacheFactory 
    = new DataCacheFactory(servers, true, true);

//get cache client for cache "NamedCache1"
DataCache myRoutingLCCacheClient 
    = myRoutingLCCacheFactory.GetCache("NamedCache1");

This example demonstrates how to use the additional parameters of the DataCacheFactory constructor when using cache notifications. The syncpolicy parameter is set to NotificationBased to enable notification-based cache invalidation. There is a 600 second localCacheTimeout value specified, but it is ignored because notifications are used to invalidate the local cache. A value of 30 seconds is specified for the pollInterval parameter. Then, this example creates a cache client for a cache that has cache notifications enabled, named NotifCache. For more information about invalidation, see Expiration and Eviction (Velocity). For more information about cache notifications, see Cache Notifications (Velocity).

Note

In order for your application to use notifications, you need to enable them on a named cache. Use the notificationsEnabled parameter with the New-Cache or Set-CacheConfig commands. For more information, see Cache Administration with PowerShell (Velocity).

'configure routing client with local cache, specify poll interval
Dim myRoutingCNCacheFactory As DataCacheFactory _
    = New DataCacheFactory(servers, True, True, _
              DataCacheLocalCacheSyncPolicy.NotificationBased, 600, 30)

'get cache client for cache "NotifCache"
Dim myRoutingCNCacheClient As DataCache _
    = myRoutingCNCacheFactory.GetCache("NotifCache")
//configure routing client with local cache, specify poll interval
DataCacheFactory myRoutingCNCacheFactory 
    = new DataCacheFactory(servers, true, true,
              DataCacheLocalCacheSyncPolicy.NotificationBased,600,30);

//get cache client for cache "NotifCache"
DataCache myRoutingCNCacheClient 
    = myRoutingCNCacheFactory.GetCache("NotifCache");

See Also

Tasks

How to: Get Started with a Simple Client (Code) (Velocity)
How to: Get Started with a Routing Client (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)