Distributed Counters with the .NET Client

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

This tutorial demonstrates how to use distributed counters with the .NET Hot Rod client, including strong counters with compare-and-swap and weak counters.

Prerequisites

  • A running Infinispan server.

  • .NET 10 or later.

Running the Example

dotnet run --project counter

Code Walkthrough

Strong Counter

Define a bounded, persistent strong counter and perform get, add, compare-and-swap, and reset operations.

var strong = counters.Counter("strong-counter");
await strong.AddAndGet(10);
await strong.CompareAndSwap(11, 50);
await strong.Reset();

Weak Counter

Weak counters support only add operations and are optimized for high-concurrency scenarios.

var weak = counters.Counter("weak-counter");
await weak.AddAndGet(5);

Expected Output

Strong counter defined.
Get: 1
AddAndGet(10): 11
CompareAndSwap(11 -> 50): old=11, swapped=True
After reset: 1

Weak counter defined.
Weak AddAndGet(5): 5

Counter names: strong-counter, weak-counter
Counters removed.