liveBook · Manning

Caching

As explained in ASP.NET Core in Action, 3e

A cache is local storage of previously retrieved data that allows subsequent requests to reuse the stored response without contacting the origin server.

Caching improves application performance by reusing previously retrieved data instead of repeating expensive work. In ASP.NET Core, caching can reduce browser requests, server response generation, or calls to remote services. section 22.1.3 section 34.1.1

Choose the caching approach according to where the stored response should live:

  • Response caching uses the ResponseCacheFilter, exposed through the [ResponseCache] attribute, to add HTTP caching headers. The client may reuse a response, but the server does not store it. section 22.1.3
  • Output caching stores generated responses on the server and reuses them for later requests. ASP.NET Core introduced it in .NET 7, with storage configurable for memory or another location such as a database. section 22.1.3
  • A background cache fetches remote data ahead of time, allowing an API endpoint to read quickly from shared cached state rather than making an HTTP call for every request. section 34.1.1

Keeping exchange-rate requests fast

Register an ExchangeRatesCache as a singleton and run an ExchangeRatesHostedService derived from BackgroundService. The service periodically calls a typed HTTP client and updates the cache; the endpoint reads directly from that cache. This keeps endpoint latency consistent while allowing the data to be refreshed. Avoid awaiting long-running work directly in StartAsync unless blocking application startup is intentional. section 34.1.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains response-versus-output behavior and implements scheduled background updates, while deferring detailed invalidation configuration.
Examples
Uses HTTP caching headers, server-stored generated responses, and a scheduled exchange-rate cache as concrete examples.
What it assumes
Assumes familiarity with ASP.NET Core endpoints and HTTP clients while teaching caching choices and hosted-service behavior.
What you can do after
Operate/configure ASP.NET Core caching and build a BackgroundService that refreshes shared data for fast API responses.
How it connects
Connects caching with response caching, output caching, action filters, background services, typed HTTP clients, and dependency injection.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.