캐시 알림 콜백 추가(Windows Server AppFabric 캐싱)

Windows Server AppFabric을 사용하면 캐시 사용 응용 프로그램이 캐시 알림을 받을 수 있습니다. 이 항목에서는 응용 프로그램이 캐시 알림을 받을 수 있도록 할 캐시 알림 콜백을 추가하는 방법에 대해 설명합니다. 캐시 알림에 대한 자세한 내용은 캐시 알림(Windows Server AppFabric 캐싱)을 참조하십시오.

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

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

참고

응용 프로그램이 알림을 사용하려면 명명된 캐시에서 알림을 사용하도록 설정해야 합니다. New-Cache 또는 Set-CacheConfig 명령에 notificationsEnabled 매개 변수를 사용합니다. 자세한 내용은 Windows PowerShell을 사용하여 Windows Server AppFabric 캐싱 기능 관리를 참조하십시오.

하나 이상의 캐시 작업에 대해 콜백을 추가하려면 다음을 수행하십시오.

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

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

    1. filter 매개 변수에 DataCacheOperations 열거를 사용하여 알림을 트리거할 캐시 작업 유형을 지정합니다. 비트 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);

참고 항목

개념

오류 알림 콜백 추가(Windows Server AppFabric 캐싱)
캐시 알림 콜백 제거(Windows Server AppFabric 캐싱)
캐시 알림 메서드(Windows Server AppFabric 캐싱)
Windows PowerShell을 사용하여 Windows Server AppFabric 캐싱 기능 관리

  2011-12-05