Share a cache between two .NET processes
Run the same console app twice against one local Squirix server. This example uses preview.10.
- First runWrites a value
- Squirix serverStores the value
- Second runReads the value
1. Start a local cache server
Use the .NET 10 SDK. In a terminal, trust the development HTTPS certificate and start the server.
Leave it running at https://localhost:5001.
dotnet dev-certs https --trust
dotnet tool install --global squirix.server.tool --version 0.1.0-preview.10
squirix-server run
2. Create a console app
Keep the server running and create the app in a second terminal.
dotnet new console --framework net10.0 -o SharedCacheDemo
dotnet add SharedCacheDemo/SharedCacheDemo.csproj package squirix --version 0.1.0-preview.10
Replace SharedCacheDemo/Program.cs with the code below. The JSON context registers the Banner type
with the SDK.
using System.Text.Json.Serialization;
using Squirix.Client;
if (args.Length != 1 || args[0] is not ("write" or "read"))
{
Console.Error.WriteLine("Usage: pass write or read as the first argument.");
return 2;
}
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var cancellationToken = timeout.Token;
await using var client = await SquirixClient.ConnectAsync(
options =>
{
options.Endpoints.Add(new Uri("https://localhost:5001"));
options.JsonSerializerContexts.Add(BannerJsonContext.Default);
},
cancellationToken);
var cache = await client.GetCacheAsync<Banner>("site-banners", cancellationToken);
if (args[0] == "write")
{
var banner = new Banner("Welcome to Squirix");
await cache.SetAsync("welcome", banner, null, cancellationToken);
Console.WriteLine($"Wrote: {banner.Message}");
return 0;
}
var result = await cache.GetValueAsync("welcome", cancellationToken);
if (!result.Found)
{
Console.Error.WriteLine("Cache miss. Run write first.");
return 1;
}
Console.WriteLine($"Read: {result.Value!.Message}");
return 0;
public sealed record Banner(string Message);
[JsonSerializable(typeof(Banner))]
internal partial class BannerJsonContext : JsonSerializerContext
{
}
3. Write, then read
dotnet run --project SharedCacheDemo -- write
dotnet run --project SharedCacheDemo -- read
Expected output:
Wrote: Welcome to Squirix
Read: Welcome to Squirix
ASP.NET Core examples
- Share typed entries with the Squirix SDK.
Write through one app instance and read through another using
ICache<T>. - Use Squirix as a HybridCache L2 backend. Share the secondary cache between two app instances using the preview.11 adapter built from source.
Persistence and replication
The quickstart stores entries in memory. Enable journal and snapshot persistence to recover entries after a cache server restart. For a replicated deployment, review the write quorum and read guarantees before choosing a topology.