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).
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
Create an array of DataCacheServerEndPoint objects to specify the cache hosts for the client.
Configure your cache hosts by assigning the cache host array from the previous step to the
serversparameter 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.
Configure your cache client type by assigning a
falsevalue to the
routingClientparameter of the DataCacheFactory constructor. Use the
truevalue for a routing client, or a
falsevalue for a simple client.
Enable local cache by assigning a
truevalue to the
localCacheparameter of the DataCacheFactory constructor.
(optional) Configure your cache client for cache notifications by using the DataCacheFactory constructor with the following additional parameters.
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).
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
syncPolicyis set to NotificationBased.
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).
Use the GetCache method to obtain an instance of the routing client.
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).
servers array is created. This example configures a cache host that is named
'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; //specify cache host(s) servers = 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
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
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).
In order for your application to use notifications, you need to enable them on a named cache. Use the
notificationsEnabled parameter with the
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");