How to: Receive Notifications from an Azure In-Role Cache
Updated: August 25, 2015
Important |
|---|
| Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me? |
This topic describes how to add a cache notification callback that will enable your application to receive cache notifications.
Adding a cache notification callback requires two steps. First, create a method that should be invoked when a cache notification is triggered by one or more cache operations. The method you invoke with the cache notifications must accept the same parameters as the DataCacheNotificationCallback delegate. Second, add a callback by using one of the three available methods from the DataCache object:
Use the filter parameter to define the types of cache operations you want to trigger cache notifications. Use the name of the method you created in the first step for the clientDelegate parameter.
-
Create the method you want to be triggered by the cache notification. Make sure the method accepts the same parameters as the DataCacheNotificationCallback delegate.
-
Add a callback. Use one of the three available methods from the DataCache object to define the notification scope: AddCacheLevelCallback, AddRegionLevelCallback, or AddItemLevelCallback.
-
Use the DataCacheOperations enumeration in the
filterparameter to specify what type of cache operations you want to trigger notifications. Select more than one enumeration by separating the enumerations with the binary OR operator to perform a bitwise OR. To do this, use the | character in C#, and the Or operator in Visual Basic. -
Use the name of the method you want to invoke when these notifications occur in the
clientDelegateparameter. -
Set the add callback method equal to a DataCacheNotificationDescriptor object that you can use elsewhere in your program to remove the cache notification callback.
-
Use the DataCacheOperations enumeration in the
The first step when registering for cache notifications is to create a method that you want to be invoked by the notification. The method called by the notification must accept the same parameters as the DataCacheNotificationCallback delegate. This example shows one example of a method that can be invoked by a cache notification.
The second step is to add a callback for one or more cache operations. In this example, a notification is created to invoke the method from the previous example. For demonstration only, this notification has been configured for all possible cache operations with a cache-level notification scope.
To define more than one cache operation, you can use the binary OR operator to assign more than one DataCacheOperations enumeration to a DataCacheOperations variable that can be used for the filter parameter. To add a callback for cache operations with a cache-level notification scope, use the AddCacheLevelCallback method.
Note |
|---|
| We do not recommend you do this in a production application. This example is for demonstration only. |
The next example shows what it looks like to add a callback for a cache operation with a region-level notification scope, that is only triggered when a region named TestRegion is added to the cache.
The next example shows what it looks like to add a callback for cache operations with an item-level notification scope, that is only triggered when an object is added or replaced in the cache using the key TestKey.
Note |
|---|
| Only item operations AddItem, ReplaceItem, and RemoveItem can trigger cache notifications with item-level callbacks. Specifying region operations in the filter parameter, when adding an item-level callback, will cause an exception. |
-
Use the RemoveCallback method to remove the cache notification callback. Use the appropriate DataCacheNotificationDescriptor object for the nd parameter. Use the NotificationDescriptor that you received when registering for notification to stop receiving notifications.
In this example, the cache client and three DataCacheNotificationDescriptor objects are declared at the class level so that they can be accessed by methods that add and remove callbacks
This example shows a method that uses the RemoveCallback method to remove the callbacks corresponding to all three DataCacheNotificationDescriptor objects from the previous example.
Important
Note