liveBook · Manning

IDistributedCache

As explained in Building Web APIs with ASP.NET Core

IDistributedCache is the .NET abstraction for storing and retrieving cached data through an external distributed cache provider.

IDistributedCache is the ASP.NET Core abstraction for storing and retrieving cached data through an external provider. Unlike in-memory caching, it lets multiple web servers share cache data without relying on local memory. section 8.0 section 8.6

Use IDistributedCache when the application needs provider-independent distributed storage:

  • Choose a DBMS such as SQL Server or a distributed key-value store such as Redis or NCache. section 8.4.1
  • Register the provider through its NuGet package; the package implements the common IDistributedCache interface. section 8.4.1
  • Add generic helper methods when working with entities, because the base interface handles strings and byte arrays rather than typed values. section 8.4.2
  • Inject IDistributedCache into a controller and use a stable key to read cached results before querying the database. section 8.4.2

Caching a paged mechanics query

A MechanicsController builds a cache key from the request type and serialized request values, then calls TryGetValue<Mechanic[]> before querying. On a miss, it filters, sorts, pages, and loads the data, stores the result for 30 seconds, and returns it. section 8.4.2

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
});

Configure a distributed SQL Server cache with the application connection string, dbo schema, and AppCache table. section 8.4.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains provider selection, registration, typed JSON extensions, dependency injection, cache keys, expiration, and controller usage.
Examples
Uses SQL Server configuration, DistributedCacheExtensions, and a MechanicsController cache around filtered and paged database results.
What it assumes
Assumes familiarity with caching and ASP.NET Core service configuration while contrasting distributed storage with in-memory caching.
What you can do after
Apply it via ASP.NET Core dependency injection to configure a provider and cache serialized application results.
How it connects
Connects distributed caching with IMemoryCache, SQL Server, Redis, dependency injection, JSON serialization, and ASP.NET Core controllers.

Also explained in

Compare how 2 books explain IDistributedCache →

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.