방법: Azure In-Role Cache에서 알림 받기

중요

Microsoft는 모든 새 개발에서 Azure Redis Cache를 사용하는 것이 좋습니다. Azure Cache 제품 선택에 대한 현재 설명서 및 지침 은 나에게 적합한 Azure Cache 제품을 참조하세요.

이 항목에서는 응용 프로그램이 캐시 알림을 받을 수 있도록 캐시 알림 콜백을 추가하는 방법에 대해 설명합니다.

캐시 알림 콜백을 추가하려면 두 단계를 수행해야 합니다. 먼저, 하나 이상의 캐시 작업에서 캐시 알림을 트리거할 때 호출할 메서드를 만듭니다. 캐시 알림을 사용하여 호출하는 메서드는 DataCacheNotificationCallback 대리자와 동일한 매개 변수를 허용해야 합니다. 둘째, DataCache 개체에서 사용 가능한 세 가지 메서드 중 하나를 사용하여 콜백을 추가합니다.

filter 매개 변수를 사용하여 캐시 알림을 트리거할 캐시 작업 유형을 정의할 수 있습니다. 첫 번째 단계에서 만든 메서드의 이름을 clientDelegate 매개 변수에 사용합니다.

하나 이상의 캐시 작업에 대해 콜백을 추가하려면

  1. 캐시 알림에서 트리거할 메서드를 만듭니다. 메서드가 DataCacheNotificationCallback 대리자와 동일한 매개 변수를 사용해야 합니다.

  2. 콜백을 추가합니다. DataCache 개체에서 사용 가능한 세 가지 방법 중 하나를 사용하여 알림 범위를 정의합니다. AddCacheLevelCallback, AddRegionLevelCallback 또는 AddItemLevelCallback.

    1. 매개 변수의 DataCacheOperations 열거형 filter 을 사용하여 알림을 트리거할 캐시 작업의 유형을 지정합니다. 비트 OR을 수행하려면 이진 OR 연산자로 열거를 구분하여 둘 이상의 열거를 선택합니다. 이렇게 하려면 C#에서는 | 문자를, Visual Basic에서는 OR 연산자를 사용합니다.

    2. 이러한 알림이 발생할 때 호출할 메서드의 이름을 clientDelegate 매개 변수에 사용합니다.

    3. 추가 콜백 메서드를 프로그램의 다른 위치에서 캐시 알림 콜백을 제거하는 데 사용할 수 있는 DataCacheNotificationDescriptor 개체와 동일하게 설정합니다.

예제

캐시 알림을 등록하는 첫 번째 단계에서는 알림에서 트리거할 메서드를 만듭니다. 알림에서 호출하는 메서드는 DataCacheNotificationCallback 대리자와 동일한 매개 변수를 사용해야 합니다. 다음 예제에서는 캐시 알림에서 호출할 수 있는 메서드의 한 예를 설명합니다.

'method invoked by notification "ndCacheLvlAllOps" 
Public Sub myCacheLvlDelegate(ByVal myCacheName As String, _
    ByVal myRegion As String, _
    ByVal myKey As String, _
    ByVal itemVersion As DataCacheItemVersion, _
    ByVal OperationId As DataCacheOperations, _
    ByVal nd As DataCacheNotificationDescriptor)

    'display some of the delegate parameters
    Console.WriteLine("A cache-level notification was triggered!")
    Console.WriteLine("    Cache: " + myCacheName)
    Console.WriteLine("    Region: " + myRegion)
    Console.WriteLine("    Key: " + myKey)
    Console.WriteLine("    Operation: " + OperationId.ToString())
    Console.WriteLine()
End Sub
//method invoked by notification "ndCacheLvlAllOps" 
public void myCacheLvlDelegate(string myCacheName,
    string myRegion, 
    string myKey, 
    DataCacheItemVersion itemVersion,
    DataCacheOperations OperationId, 
    DataCacheNotificationDescriptor nd)
{
    //display some of the delegate parameters
    Console.WriteLine("A cache-level notification was triggered!");
    Console.WriteLine("    Cache: " + myCacheName);
    Console.WriteLine("    Region: " + myRegion);
    Console.WriteLine("    Key: " + myKey);
    Console.WriteLine("    Operation: " + OperationId.ToString());
    Console.WriteLine();
}

두 번째 단계에서는 하나 이상의 캐시 작업에 대해 콜백을 추가합니다. 다음 예제에서는 위 예제의 메서드를 호출할 알림을 만듭니다. 데모 목적으로만 사용되는 이 알림은 캐시 수준 알림 범위로 가능한 모든 캐시 작업에 대해 구성되었습니다.

캐시 작업을 둘 이상 정의하려면 이진 OR 연산자를 사용하여 필터 매개 변수에 사용할 수 있는 DataCacheOperations 변수에 둘 이상의 DataCacheOperations 열거를 할당할 수 있습니다. 캐시 수준 알림 범위로 캐시 작업에 대한 콜백을 추가하려면 AddCacheLevelCallback 메서드를 사용합니다.

참고

프로덕션 응용 프로그램에서는 이 작업을 수행하지 않는 것이 좋습니다. 이 예제는 데모 목적으로만 사용됩니다.

'specify all possible item and region operations
Dim allCacheOperations As DataCacheOperations
allCacheOperations = DataCacheOperations.AddItem Or _
    DataCacheOperations.ReplaceItem Or _
    DataCacheOperations.RemoveItem Or _
    DataCacheOperations.CreateRegion Or _
    DataCacheOperations.ClearRegion Or _
    DataCacheOperations.RemoveRegion

'add cache-level notification callback 
'all cache operations from a notifications-enabled cache
Dim ndCacheLvlAllOps as DataCacheNotificationDescriptor = _
    myTestCache.AddCacheLevelCallback(allCacheOperations, AddressOf myCacheLvlDelegate)
//specify all possible item and region operations
DataCacheOperations allCacheOperations = DataCacheOperations.AddItem |
    DataCacheOperations.ReplaceItem |
    DataCacheOperations.RemoveItem |
    DataCacheOperations.CreateRegion |
    DataCacheOperations.ClearRegion |
    DataCacheOperations.RemoveRegion;

//add cache-level notification callback 
//all cache operations from a notifications-enabled cache
DataCacheNotificationDescriptor ndCacheLvlAllOps
    = myTestCache.AddCacheLevelCallback(allCacheOperations, myCacheLvlDelegate);

다음 예제에서는 영역 수준 알림 범위로 캐시 작업에 대한 콜백을 추가하는 방법을 설명합니다. 이 콜백은 TestRegion이라는 영역을 캐시에 추가하는 경우에만 트리거됩니다.

'add region-level notification callback for region "TestRegion"
'trigger notification with CreateRegion operation
Dim ndRegionCreateRegOp As DataCacheNotificationDescriptor
ndRegionCreateRegOp = _
    myTestCache.AddRegionLevelCallback("TestRegion", _
    DataCacheOperations.CreateRegion, AddressOf myRegionLvlAddDelegate)
//add region-level notification callback for region "TestRegion"
//trigger notification with CreateRegion operation
DataCacheNotificationDescriptor ndRegionCreateRegOp
    = myTestCache.AddRegionLevelCallback("TestRegion",
    DataCacheOperations.CreateRegion, myRegionLvlAddDelegate);

다음 예제에서는 항목 수준 알림 범위로 캐시 작업에 대한 콜백을 추가하는 방법을 설명합니다. 이 콜백은 TestKey 키를 사용하여 캐시에 개체를 추가하거나 바꾸는 경우에만 트리거됩니다.

참고

AddItem, ReplaceItemRemoveItem 항목 작업에서만 항목 수준 콜백으로 캐시 알림을 트리거할 수 있습니다. 항목 수준 콜백을 추가할 때 필터 매개 변수에 영역 작업을 지정하면 예외가 발생합니다.

'add item-level notification callback for item "TestKey"
'trigger notification with AddItem and ReplaceItem operations
Dim ndItemUpdateOps As DataCacheNotificationDescriptor
ndItemUpdateOps = _
    myTestCache.AddItemLevelCallback("TestKey", _
    (DataCacheOperations.AddItem Or DataCacheOperations.ReplaceItem), _
    AddressOf myItemLvlUpdateDelegate)
//add item-level notification callback for item "TestKey"
//trigger notification with AddItem and ReplaceItem operations
DataCacheNotificationDescriptor ndItemUpdateOps
    = myTestCache.AddItemLevelCallback("TestKey",
        (DataCacheOperations.AddItem | DataCacheOperations.ReplaceItem),
        myItemLvlUpdateDelegate);

콜백 캐시 알림을 제거하려면

  1. RemoveCallback 메서드를 사용하여 캐시 알림 콜백을 제거합니다. nd 매개 변수에 적절한 DataCacheNotificationDescriptor 개체를 사용합니다. 알림 수신을 중지하려면 알림에 등록할 때 받은 NotificationDescriptor를 사용합니다.

예제

이 예제에서는 캐시 클라이언트와 세 개의 DataCacheNotificationDescriptor 개체가 클래스 수준에서 선언되므로 콜백을 추가하고 제거하는 메서드로 액세스할 수 있습니다.

'define variables for class
Dim myTestCache As DataCache
Dim ndCacheLvlAllOps As DataCacheNotificationDescriptor
Dim ndRegionLvlAllOps As DataCacheNotificationDescriptor
Dim ndItemLvlAllOps As DataCacheNotificationDescriptor
//define variables for class
DataCache myTestCache;
DataCacheNotificationDescriptor ndCacheLvlAllOps;
DataCacheNotificationDescriptor ndRegionLvlAllOps;
DataCacheNotificationDescriptor ndItemLvlAllOps;

이 예제에서는 RemoveCallback 메서드를 사용하여 이전 예제에서 세 개의 DataCacheNotificationDescriptor 개체 모두에 해당하는 콜백을 제거하는 방법을 보여 줍니다.

'remove cache notification callbacks
Public Sub RemoveNotificationCallbacks()
    myTestCache.RemoveCallback(ndCacheLvlAllOps)
    myTestCache.RemoveCallback(ndRegionLvlAllOps)
    myTestCache.RemoveCallback(ndItemLvlAllOps)
End Sub
//remove cache notification callbacks
public void RemoveNotificationCallbacks()
{
    myTestCache.RemoveCallback(ndCacheLvlAllOps);
    myTestCache.RemoveCallback(ndRegionLvlAllOps);
    myTestCache.RemoveCallback(ndItemLvlAllOps);
}