Skip To Content

Distributed Cache For ASP.NET Core

Share typed cache entries between two ASP.NET Core instances using Squirix.

Run two instances of the same ASP.NET Core application against one Squirix server. Write a banner through the first instance, then read it through the second. Both instances use the same cache name and key.

The application uses the Squirix SDK directly. This example does not configure ASP.NET Core session state, output caching, or an IDistributedCache provider.

Get the runnable sample

Open the complete sample on GitHub. Its README contains the setup and run commands. The sample targets .NET 10 and references the published Squirix package 0.1.0-preview.10.

Connect both instances to Squirix

Each application process opens its own client connection to https://localhost:5001. Register the JSON context for the shared type and open the same named cache in both instances:

using var startupTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var client = await SquirixClient.ConnectAsync(
    options =>
    {
        options.Endpoints.Add(new Uri(endpoint));
        options.JsonSerializerContexts.Add(BannerJsonContext.Default);
    },
    startupTimeout.Token).ConfigureAwait(false);
await using var clientLifetime = client.ConfigureAwait(false);
var cache = await client.GetCacheAsync<Banner>(
    "shared-banner-demo", startupTimeout.Token).ConfigureAwait(false);

These are excerpts from the complete project. The project defines Banner, its source-generated JSON context, and the configurable server endpoint. Keep the client alive until the application stops.

Write and read through application endpoints

The PUT endpoint writes a typed banner. The GET endpoint returns the stored value, or HTTP 404 when the key is absent. Both forward the request cancellation token to the SDK.

_ = app.MapPut("/banner/{key}", async (string key, Banner banner, CancellationToken cancellationToken) =>
{
    await cache.SetAsync(key, banner, null, cancellationToken).ConfigureAwait(false);
    return Results.NoContent();
});

_ = app.MapGet("/banner/{key}", async (string key, CancellationToken cancellationToken) =>
{
    var result = await cache.GetValueAsync(key, cancellationToken).ConfigureAwait(false);
    return result.Found ? Results.Ok(result.Value) : Results.NotFound();
});

Run and verify

Follow the sample README to trust the development HTTPS certificate, start the Squirix server, and build the app. In the sample directory, start two instances in separate terminals:

dotnet run --no-build --no-launch-profile --urls http://localhost:5101
dotnet run --no-build --no-launch-profile --urls http://localhost:5102

In a Bash terminal, check a missing key, write through port 5101, then read through port 5102:

curl -i http://localhost:5102/banner/not-written
curl -i -X PUT http://localhost:5101/banner/welcome -H "Content-Type: application/json" -d '{"message":"Hello from the first instance"}'
curl -i http://localhost:5102/banner/welcome

Expect HTTP 404, then HTTP 204, then HTTP 200 with this JSON:

{"message":"Hello from the first instance"}

The application endpoints use HTTP on loopback for local development. The Squirix connections use HTTPS with certificate validation enabled. Stop all three processes with Ctrl+C when finished. The server runs in memory by default; stopping it clears the cache.

Next steps

See Getting started for server setup, Client API for cache operations, and the SDK package on NuGet.