添加缓存通知回拨(Windows Server AppFabric 缓存功能)

Windows Server AppFabric 可以使启用缓存的应用程序接收缓存通知。本主题介绍了如何添加将启用应用程序以接收缓存通知的缓存通知回拨。有关缓存通知的详细信息,请参阅缓存通知(Windows Server AppFabric 缓存)

添加缓存通知回拔需要完成以下两个步骤:首先,创建当缓存通知由一个或多个缓存操作触发时,应调用的方法。您使用缓存通知调用的方法必须接受与 DataCacheNotificationCallback 委派相同的参数。第二,通过使用 DataCache 对像的三种可用方法之一添加回拔:

使用 filter 参数定义您要触发缓存通知的缓存操作的类型。为 clientDelegate 参数使用您在步骤一中创建的方法的名称。

备注

要想让您的应用程序使用通知,需要在命名缓存中启用它们。对 New-CacheSet-CacheConfig 命令使用 notificationsEnabled 参数。有关详细信息,请参阅使用 Windows PowerShell 管理 Windows Server AppFabric 缓存功能

为一个或多个缓存操作添加回拔

  1. 创建您要通过缓存通知触发的方法。确保此方法接受与 DataCacheNotificationCallback 委派相同的参数。

  2. 添加回拔。使用 DataCache 对像的三种可用方法之一定义通知作用域:AddCacheLevelCallbackAddRegionLevelCallbackAddItemLevelCallback

    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 运算符为可用于 filter 参数的 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 在缓存中添加或替换对像时才触发。

备注

只有项目操作 AddItemReplaceItemRemoveItem 才能触发带有项目级别回调的缓存通知。添加项目级别回调时,如果指定 filter 参数中的区域操作,将导致异常。

'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