This documentation is archived and is not being maintained.

How to: Receive Notifications from an Azure In-Role Cache

Updated: August 25, 2015

ImportantImportant
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.

  1. Create the method you want to be triggered by the cache notification. Make sure the method accepts the same parameters as the DataCacheNotificationCallback delegate.

  2. Add a callback. Use one of the three available methods from the DataCache object to define the notification scope: AddCacheLevelCallback, AddRegionLevelCallback, or AddItemLevelCallback.

    1. Use the DataCacheOperations enumeration in the filter parameter 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.

    2. Use the name of the method you want to invoke when these notifications occur in the clientDelegate parameter.

    3. Set the add callback method equal to a DataCacheNotificationDescriptor object that you can use elsewhere in your program to remove the cache notification callback.

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.

No code example is currently available or this language may not be supported.

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.

noteNote
We do not recommend you do this in a production application. This example is for demonstration only.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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.

noteNote
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.

No code example is currently available or this language may not be supported.

  1. 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

No code example is currently available or this language may not be supported.

This example shows a method that uses the RemoveCallback method to remove the callbacks corresponding to all three DataCacheNotificationDescriptor objects from the previous example.

No code example is currently available or this language may not be supported.

Show: