Enable Windows Server AppFabric Local Cache
Windows Server AppFabric 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 Windows Server AppFabric 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 (Windows Server AppFabric 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 (Windows Server AppFabric 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 (Windows Server AppFabric Caching).
MaxQueueLengthparameter controls the queue length for notifications, but it does not specifically affect the local cache. The default value is 16384.
- 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.
Use the GetCache method to obtain a DataCache class based on the settings of the DataCacheFactoryConfiguration object.
|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 Windows Server AppFabric 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 Windows Server AppFabric Caching Physical Architecture Diagram. 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 Windows Server AppFabric Caching Features.First, the servers array is created. This example configures a cache host that is named
// Declare array for cache host(s). DataCacheServerEndpoint servers = new DataCacheServerEndpoint; servers = 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. 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
// 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
// Create a configured DataCacheFactory object. DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig); // Get a cache client for the cache "NamedCache1". 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 Windows Server AppFabric Cache Client
Change the Cache Client Logging Level (Windows Server AppFabric Caching)
Cache Clients and Local Cache (Windows Server AppFabric Caching)
Windows Server AppFabric Caching Concepts
Developing a Cache Client (Windows Server AppFabric Caching)