How to: Enable Local Cache (Code) (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
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 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.
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.
Enable local cache by assigning a true value to the localCache parameter 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 syncPolicy is 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.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Application Configuration Code client parameter enable local localcacheHow to: Get Started with a Simple Client (Code) (Velocity)
When you programmatically configure your cache client, the configuration settings are passed to the DataCacheFactory class 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.
The cache client type is defined by the routingClient parameter in the DataCacheFactory class constructor. For a simple client, the routingClient parameter must be false. 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 configure a simple client programmatically
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 servers parameter of the DataCacheFactory constructor.
Select a simple client type by assigning a false value to the routingClient parameter of the DataCacheFactory constructor.
Configure local cache by assigning a true or false value to the localCache parameter of the DataCacheFactory constructor. Use the true value to enable local cache or a false value to disable local cache.
Use the GetCache method to obtain an instance of the routing client.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Configuration Code client class datacachefactory constructor configure simpleHow to: Get Started with a Routing Client (Code) (Velocity)
When you programmatically configure your cache client, the configuration settings are passed to the DataCacheFactory class 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.
The cache client type is defined by the routingClient parameter in the DataCacheFactory class constructor. For a routing client, the routingClient parameter must be 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 configure a routing client programmatically
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 servers parameter of the DataCacheFactory constructor.
Select a routing client type by assigning a true value to the routingClient parameter of the DataCacheFactory constructor.
Configure local cache by assigning a true or false value to the localCache parameter of the DataCacheFactory constructor. Use the true value to enable local cache, or a false value to disable local cache.
Use the GetCache method to obtain an instance of the routing client.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Configuration Code client class datacachefactory constructor configure routingHow to: Remove an Object from Cache (Velocity)
The following examples show the ways you can remove objects from the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).
For more details about the methods that are used in these examples, see these class library topics:
Remove
Item
Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.
To remove an object from the cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Remove method or Item property may be used to remove an object from cache.
Example
The following example uses the Remove method to remove an object from cache.
Note
There are many other parameters available for this method. See the Remove class library for more information.
C# Copy Code
//remove object in cache using key "Key0"
myCache.Remove("Key0");
The following example uses the Item property that has array notation to remove an object from cache.
C# Copy Code
//remove object in cache using array notation
myCache["Key0"] = null;
How to: Update an Object in Cache (Velocity)
To update an object in cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Put method or Item property may be used to update an object in cache.
Example
The following example uses the Put method to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
Note
There are many other parameters available for this method. See the Put class library for more information.
C# Copy Code
//add or replace object in cache using key "Key0"
myCache.Put("Key0", "object replaced or added using Key0");
The following example uses the Item property that has array notation to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
C# Copy Code
//add or replace object in cache using array notation
myCache["Key0"] = "object replaced or added using Key0";
How to: Add an Object to Cache (Velocity)
The following examples show the ways you can add objects to the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).
For more details about the methods that are used in these examples, follow these links to view the class library topics:
Add
Put
Item
Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.
To add an object to cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Add method, Put method, or Item property may be called to add an object to the cache.
Example
The following example demonstrates how you can use the Add method to add an object to cache. You will get an exception if an object has already been cached by the same key (the first parameter).
Note
There are many other parameters available for this method. See the Add class library for more information.
C# Copy Code
//add object to cache with key "Key0"
myCache.Add("Key0", "object added with Key0");
Cache Notifications (Velocity)
To receive asynchronous cache notifications, add a cache notification callback to your application. When you add the callback, you define the types of cache operations that trigger a cache notification and which method in your application should be called when the specified operations occur. This topic describes the process in detail.
Note
In order to use cache notifications, you will have to enable cache notifications on a named cache with the New-Cache or Set-CacheConfig PowerShell cmdlets from the PowerShell-based cache administration tool. You also need to configure your application to use a routing client.
Triggering Cache Notifications
As shown in the following figure, changes to both regions and cached objects (referred to as items within the cache) can trigger cache notifications.
These cache operations are defined by members of the DataCacheOperation class.
Region Operations
Your application can receive cache notifications when the following cache operations occur on a region.
CreateRegion: When a region is created in the cache.
ClearRegion: When a region is cleared in the cache.
RemoveRegion: When a region is removed from the cache.
Item Operations
Your application may receive cache notifications when the following cache operations occur on a cached object (referred to as an item within the cache).
AddItem: When an item is added to the cache.
ReplaceItem: When an item is replaced in the cache.
RemoveItem: When an item is removed from the cache.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 Distribute Cache Cache NotificationWhat Happens When a Cache Host Fails
If a cache host fails (assuming there are still a sufficient number of cache hosts available to keep the cluster running) nothing changes for the cache-enabled application. The cache cluster re-routes requests for the object to the cache host that maintained the secondary copy of the object. Within the cluster, the secondary copies of all the primary objects are then elevated to become the new primary objects. Then, secondary copies of those new primary objects are distributed to other cache hosts across the cluster. Secondary objects on the cache host that failed are replaced by new secondary objects and distributed across the cluster. This process also applies to regions.
For the high availability feature to help insulate your application from the failure of a cache host, at least three cache hosts must be members of the cache cluster. This is due to a strong consistency requirement stating that there must always be two copies of a cached object or region in a high availability-enabled cache. In order to maintain two copies of a cache or region, a high availability-enabled cache requires at least two cache hosts to function.
For example, perhaps you have created a high availability-enabled cached named HACache in a three-server cache cluster as shown in the following table. Assume that SQL Server was configured to perform the cluster management role (so that this example does not need to consider the potential loss of lead hosts).
| Time | Cache host 1 | Cache host 2 | Cache host 3 | HACache (high availability-enabled named cache) |
| T1 | running | running | running | available |
| T2 | running | running | stopped | available |
| T3 | running | stopped | stopped | not available |
At T1, when there are three cache hosts available, two copies of cached objects or regions can be stored on one of three available servers. At T2, when one cache server fails, HACache continues to be available because there are still two cache hosts available to store the two copies of cached objects or regions. At T3, when the second cache host fails, HACache becomes unavailable. This is because there is no longer another cache host available to store the second copy of cached objects or regions.