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:
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.
Pass the DataCacheLocalCacheProperties object to the constructor of a new DataCacheFactory object.
Use the DataCacheFactory object to call the GetCache method to create a cache client that uses local cache.
|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
Create an array of DataCacheServerEndpoint objects to specify the cache hosts for the client.
Create an instance of the DataCacheLocalCacheProperties class. Configure the local cache by passing appropriate values to the parameters of the constructor.
objectCountparameter to specify the maximum number of objects in the local cache.
defaultTimeoutparameter to specify a System.TimeSpan object that determines the time that an object will remain in local cache before it is invalidated.
invalidationPolicyparameter 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).
- Use the
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.
PollIntervalparameter 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).
MaxQueueLengthparameter controls the queue length for notifications, but it does not specifically affect the local cache. The default value is 10000.
- Use the
Create an instance of the DataCacheFactoryConfiguration class.
Configure your cache hosts by assigning the cache host array from the first step to the
Serversproperty of the DataCacheFactoryConfiguration object.
Configure the local cache by assigning the DataCacheLocalCacheProperties object created in the second step to the
LocalCachePropertiesproperty of the DataCacheFactoryConfiguration object.
If needed, configure the notification properties by assigning the DataCacheNotificationProperties object created in the third step to the
NotificationPropertiesproperty of the DataCacheFactoryConfiguration object.
Pass the DataCacheFactoryConfiguration object to the constructor of the DataCacheFactory class.
|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.|
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
' Declare array for cache host(s). Dim servers(0) As DataCacheServerEndpoint 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
' 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)
Next create a DataCacheFactoryConfiguration object. Assign the
servers array to the
Servers property. Assign the
localCacheConfig object to the
' Setup the DataCacheFactory configuration. Dim factoryConfig As 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
' 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")
|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.|
ConceptsGet 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