0Pricing
C# Academy · Lesson

Output Caching Basics

Cache responses to reduce work.

Output Caching Basics is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Output Caching?

Output caching stores the full HTTP response of an endpoint so that repeated requests are served from the cache without re-running the handler. This cuts latency and load dramatically for read-heavy endpoints.

// First call -> handler runs, response stored
// Next calls -> response served from cache

Output Cache vs Response Cache

Built-in output caching (added in .NET 7) stores responses on the server and you fully control it. The older response caching only emits HTTP cache headers and relies on the client or proxies to honor them.

// Output caching: server-side store you control
// Response caching: just Cache-Control headers

AddOutputCache

Register the service with AddOutputCache.

builder.Services.AddOutputCache();

UseOutputCache

Add the middleware to the pipeline with UseOutputCache. Place it after routing and, if present, after CORS.

var app = builder.Build();

app.UseOutputCache();

app.MapControllers();

CacheOutput on an Endpoint

Opt an endpoint into caching with CacheOutput (minimal API) or the [OutputCache] attribute (controllers).

app.MapGet("/products", GetProducts)
   .CacheOutput();

// Controller:
// [OutputCache]
// public IActionResult Get() => Ok(...);

Setting an Expiration

Control how long a response stays cached with Expire.

app.MapGet("/products", GetProducts)
   .CacheOutput(policy => policy.Expire(TimeSpan.FromSeconds(30)));

What Gets Cached

By default only GET and HEAD requests with a 200 status are cached, and only when there is no Set-Cookie or authorization involved - caching authenticated, user-specific responses by mistake is dangerous.

// Cached by default: GET/HEAD, 200, no cookies/auth

Varying by Query String

Two requests to the same path with different query strings should usually be cached separately. Use SetVaryByQuery.

app.MapGet("/search", Search)
   .CacheOutput(policy => policy
        .Expire(TimeSpan.FromSeconds(60))
        .SetVaryByQuery("q", "page"));

Varying by Header

You can vary the cache key by headers - for example, to serve a different cached copy per Accept-Language.

policy.SetVaryByHeader("Accept-Language");

A Base Policy for Everything

AddBasePolicy applies a default to every endpoint, which individual endpoints can still override.

builder.Services.AddOutputCache(options =>
{
    options.AddBasePolicy(policy =>
        policy.Expire(TimeSpan.FromSeconds(10)));
});

Disabling for an Endpoint

If a base policy caches everything, exclude dynamic endpoints with NoCache.

app.MapGet("/cart", GetCart)
   .CacheOutput(policy => policy.NoCache());

Quick Check

Confirm the difference between the two caching features.

Recap

You learned output caching basics:

  • AddOutputCache + UseOutputCache enable server-side caching.
  • CacheOutput / [OutputCache] opt endpoints in; Expire sets duration.
  • Only GET/HEAD 200 responses without auth/cookies are cached by default.
  • SetVaryByQuery and SetVaryByHeader shape the cache key.

Next: cache policies and invalidation.

Frequently asked questions

Is the “Output Caching Basics” lesson free?

Yes — the full text of “Output Caching Basics” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Output Caching Basics”?

Cache responses to reduce work. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Output Caching Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Rate Limiting Algorithms
  2. Configuring Rate Limiting Middleware
  3. Output Caching Basics
  4. Cache Policies and Invalidation
← Back to C# Academy