Cache Event Listeners with the .NET Client

Tip
The source code for this tutorial is available on GitHub.

This tutorial demonstrates how to register a cache event listener that receives notifications when entries are created, modified, or removed.

Prerequisites

  • A running Infinispan server.

  • .NET 10 or later.

Running the Example

dotnet run --project listeners

Code Walkthrough

Implementing a Listener

Implement the IClientListener interface to handle cache events.

class EventPrinter : IClientListener
{
    public string ListenerID { get; set; }

    public void OnEvent(Event e)
    {
        var key = Encoding.UTF8.GetString(e.Key);
        Console.WriteLine($"{e.Type} {key}");
    }

    public void OnError(Exception ex) { }
}

Registering and Removing

Use AddListener to start receiving events and RemoveListener to stop.

await cache.AddListener(listener);
// ... perform operations ...
await cache.RemoveListener(listener);

Expected Output

Listener registered.
CREATED key1
CREATED key2
MODIFIED key1
Listener removed.