liveBook · Manning

Distributed caching

As explained in Building Web APIs with ASP.NET Core

Distributed caching is a caching technique that stores reusable data in shared storage so multiple application instances can access it.

Distributed caching stores reusable data in a shared repository that multiple web servers can access. A local in-memory cache belongs to one server: another server must create its own entry, possibly from data retrieved at a different time. A shared cache avoids those conflicting copies and can survive a web-server restart without using the servers’ local memory. section 8.4

For a board-game API, distributed caching is useful when repeated requests would otherwise query the database. Cache at the highest practical level: HTTP response caching suits frequently requested responses that stay the same, while server-side in-memory or distributed caching can reduce database work for requests with varying parameters. These techniques can be combined. section 8.1 section 8.6

Choose an external cache provider, then register its service in ASP.NET Core:

  • SQL Server can use an existing database connection, but needs a dedicated cache table. Install Microsoft.Extensions.Caching.SqlServer, register AddDistributedSqlServerCache, and create the table with the dotnet-sql-cache tool. section 8.4.2
  • Redis is a distributed key-value store. Set up a Redis instance, install its provider package, keep its connection string in User Secrets, and register AddStackExchangeRedisCache instead of the SQL Server provider. section 8.4.1 section 8.4.3
Listing 8.7 Program.cs file: Distributed SQL Server cache service
builder.Services.AddDistributedSqlServerCache(options =>                  #1
{
    options.ConnectionString = 
        builder.Configuration.GetConnectionString("DefaultConnection");   #2
    options.SchemaName = "dbo";                                           #2
    options.TableName = "AppCache";                                       #2
});

The SQL Server registration reuses the configured database connection and directs cache entries to the dedicated dbo.AppCache table. Create that table separately with dotnet sql-cache create before using it. section 8.4.2

Cache a board-game API query

Inject IDistributedCache into a controller. For a mechanics GET request, form a cache key from the request type and serialized input so different request parameters use different entries. Try to retrieve the cached Mechanic array; on a miss, run the ordered, paged database query and store its result for 30 seconds. The cache helper serializes values to bytes and deserializes them on retrieval. section 8.4.2

builder.Services.AddStackExchangeRedisCache(options =>     #1
{
    options.Configuration = 
        builder.Configuration["Redis:ConnectionString"];   #2
});

To switch providers, register the Redis cache with its connection string in place of the SQL Server registration. The controller continues using IDistributedCache, so its caching code does not need to change. section 8.4.3

Check setup before relying on the cache: the SQL Server table must exist, or the Redis connection settings must point to an accessible instance. For SQL Server practice, change the configured table from AppCache to SQLCache and create the new table with the CLI; leave the old table available so the change can be reversed. section 8.4.2 section 8.5.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through SQL Server setup, cache helpers, controller use, and Redis substitution; leaves other SQL provider options unexplained.
Examples
Uses a cached, paginated mechanics GET request, SQL Server cache-table configuration, Redis registration, and a table-renaming exercise.
What it assumes
Assumes an existing ASP.NET Core API, database connection, and in-memory cache; teaches provider registration and SQL cache-table creation.
What you can do after
Apply it via IDistributedCache with a SQL Server or Redis provider to cache API query results across servers.
How it connects
Connects in-memory caching, HTTP response caching, database queries, SQL Server, Redis, and IDistributedCache.

Also explained in

Compare how 4 books explain distributed caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.