Use Squirix as a HybridCache L2 Backend

Run two ASP.NET Core processes and verify that the second reuses a value the first cached in Squirix.

1. Build the Sample

Squirix 0.1.0-preview.11 includes an IDistributedCache adapter. This example builds the server and adapter from a fixed source revision. Use Git, the .NET 11 SDK, and the ASP.NET Core 10 runtime. The sample targets .NET 10.

git clone https://github.com/squirix/squirix.git squirix-hybridcache-demo && cd squirix-hybridcache-demo
git checkout --detach 31160471d0a70aec4cb9c5a199611a9faddaf063
dotnet dev-certs https --trust
dotnet build src/squirix.server.host/Squirix.Server.Host.csproj
dotnet build samples/aspnet-hybrid-cache/AspNetHybridCache.csproj

Both app instances use the same registration. AddSquirixCache registers the shared IDistributedCache backend. AddHybridCache keeps its own in-memory cache in each process.

builder.Services.AddSquirixCache(options =>
    options.ClientOptions.Endpoints.Add(new Uri("https://localhost:5443")));
builder.Services.AddHybridCache();

If your app already uses AddStackExchangeRedisCache with Redis or Garnet, replace that registration with AddSquirixCache and keep your existing AddHybridCache() and GetOrCreateAsync calls. Deploy the change to all app instances together. The new backend starts with a separate set of cache entries.

2. Start One Server and Two App Instances

Run each command in a separate terminal from the cloned repository root. Leave all three running. You can substitute other free ports. Keep the Squirix URL and both --CacheEndpoint values in sync.

Squirix Server

dotnet run --no-build --no-launch-profile --project src/squirix.server.host -- run --urls https://localhost:5443

ASP.NET Core Instance A

dotnet run --no-build --no-launch-profile --project samples/aspnet-hybrid-cache -- --urls http://localhost:5101 --CacheEndpoint https://localhost:5443

ASP.NET Core Instance B

dotnet run --no-build --no-launch-profile --project samples/aspnet-hybrid-cache -- --urls http://localhost:5102 --CacheEndpoint https://localhost:5443

The demo endpoints use HTTP on loopback. Both app instances connect to Squirix over HTTPS using the trusted development certificate.

3. Verify the Shared L2

Open these addresses in order while the shared entry is still fresh. The sample includes the process ID in each value it creates.

  1. http://localhost:5101/messages/demo creates the value in instance A.
  2. http://localhost:5102/messages/demo returns the same value, including A's process ID.
  3. http://localhost:5102/messages/second-only creates a different value with B's process ID.

On the second request, B has never seen demo, so its local cache is empty for that key. It returns A's value from Squirix. The third request confirms that B has its own process ID. Your process IDs will differ from run to run.

To repeat the check, replace demo with a fresh key in both URLs. Stop all three processes with Ctrl+C when finished.

Local and Shared Expiration

The sample keeps each entry in Squirix for five minutes and in each process for two seconds. A change to a shared entry can leave an older copy in another process's local cache until that copy expires. HybridCache also coalesces concurrent cache misses for a key within each process.

Sample source · Squirix adapter guide · Microsoft HybridCache documentation