Jaa


Enable AppFabric 1.1 Local Cache

Microsoft AppFabric 1.1 for Windows Server 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 Enable Local Cache (XML).

The following steps describe the process required to programmatically enable local cache for your cache client:

  1. Create an instance of the DataCacheLocalCacheProperties class. Configure the local cache by passing appropriate values to the parameters of the constructor. These include objectCount, defaultTimeout, and invalidationPolicy.

  2. Pass the DataCacheLocalCacheProperties object to the constructor of a new DataCacheFactory object.

  3. Use the DataCacheFactory object to call the GetCache method to create a cache client that uses local cache.

Warning

These procedures assume that you have already prepared your development environment and set references to the AppFabric Caching assemblies. For more information, see Preparing the Cache Client Development Environment (AppFabric 1.1 Caching).

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. Create an instance of the DataCacheLocalCacheProperties class. Configure the local cache by passing appropriate values to the parameters of the constructor.

    1. Use the objectCount parameter to specify the maximum number of objects in the local cache.

    2. Use the defaultTimeout parameter to specify a System.TimeSpan object that determines the time that an object will remain in local cache before it is invalidated.

    3. Use the invalidationPolicy parameter to specify how locally cached objects are invalidated. Specify DataCacheLocalCacheInvalidationPolicy.TimeoutBased to indicate that only the time-out value should be used. Specify DataCacheLocalCacheInvalidationPolicy.NotificationBased to indicate that cache notifications will be used in addition to time-outs. For more information, see Cache Clients and Local Cache (AppFabric 1.1 Caching).

  3. If you selected DataCacheLocalCacheInvalidationPolicy.NotificationBased for the invalidation policy, you can optionally control the polling interval for how often the cache client communicates with the cache cluster for update notifications for locally cached objects. To do this, create an instance of the DataCacheNotificationProperties class. Configure the notification settings by passing appropriate values to the parameters of the constructor.

    1. Use the PollInterval parameter to specify a System.Timespan object for the interval of frequency that the cache client will check with the cache cluster for cache notifications. Note that local cache is not required for cache notifications. For more information, see Cache Notifications (AppFabric 1.1 Caching).

    2. The MaxQueueLength parameter controls the queue length for notifications, but it does not specifically affect the local cache. The default value is 10000.

  4. Create an instance of the DataCacheFactoryConfiguration class.

  5. Configure your cache hosts by assigning the cache host array from the first step to the Servers property of the DataCacheFactoryConfiguration object.

  6. Configure the local cache by assigning the DataCacheLocalCacheProperties object created in the second step to the LocalCacheProperties property of the DataCacheFactoryConfiguration object.

  7. If needed, configure the notification properties by assigning the DataCacheNotificationProperties object created in the third step to the NotificationProperties property of the DataCacheFactoryConfiguration object.

  8. Pass the DataCacheFactoryConfiguration object to the constructor of the DataCacheFactory class.

  9. Use the GetCache method to obtain a DataCache class based on the settings of the DataCacheFactoryConfiguration object.

Note

If you selected DataCacheLocalCacheInvalidationPolicy.NotificationBased for the local cache invalidation policy, the target cache must be configured to have notifications enabled. For more information, see Using Windows PowerShell to Manage AppFabric 1.1 Caching Features.

Example

This example shows the programmatic configuration of a cache client that has local cache enabled. This client is configured to use a cache called NamedCache1 and 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 AppFabric Caching Physical Architecture Diagram (AppFabric 1.1 Caching). 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 AppFabric 1.1 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 DataCacheLocalCacheProperties object. In this example, the local cache is created with an object count of 10000 and a time-out of 30 seconds.

' Set the local cache properties. In this example, it
' is timeout-based with a timeout of 30 seconds.
Dim localCacheConfig As DataCacheLocalCacheProperties
Dim localTimeout As TimeSpan = New TimeSpan(0, 0, 30)

localCacheConfig = New DataCacheLocalCacheProperties(10000, _
   localTimeout, DataCacheLocalCacheInvalidationPolicy.TimeoutBased)
// Set the local cache properties. In this example, it
// is timeout-based with a timeout of 30 seconds.
DataCacheLocalCacheProperties localCacheConfig;
TimeSpan localTimeout = new TimeSpan(0, 0, 30);

localCacheConfig = new DataCacheLocalCacheProperties(10000, 
   localTimeout, DataCacheLocalCacheInvalidationPolicy.TimeoutBased);

Next create a DataCacheFactoryConfiguration object. Assign the servers array to the Servers property. Assign the localCacheConfig object to the LocalCacheProperties property.

' Setup the DataCacheFactory configuration.
Dim factoryConfig As DataCacheFactoryConfiguration
factoryConfig = New DataCacheFactoryConfiguration

factoryConfig.Servers = servers
factoryConfig.LocalCacheProperties = localCacheConfig
// Setup the DataCacheFactory configuration.
DataCacheFactoryConfiguration factoryConfig = 
   new DataCacheFactoryConfiguration();

factoryConfig.Servers = servers;
factoryConfig.LocalCacheProperties = localCacheConfig;

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");

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.

See Also

Concepts

Get Started with a Cache Client
Change the Cache Client Logging Level
Cache Clients and Local Cache (AppFabric 1.1 Caching)
AppFabric Caching Concepts (AppFabric 1.1 Caching)
Developing a Cache Client

Other Resources

Configuring the Cache Client with XML

  2012-09-12