0Pricing
C# Academy · Lesson

Cache Policies and Invalidation

Control cache lifetime and tags.

Named Policies

Rather than repeating cache settings on every endpoint, define reusable named policies once and reference them by name.

builder.Services.AddOutputCache(options =>
{
    options.AddPolicy("Short", policy =>
        policy.Expire(TimeSpan.FromSeconds(10)));
    options.AddPolicy("Long", policy =>
        policy.Expire(TimeSpan.FromMinutes(10)));
});

Applying a Named Policy

Reference the policy by its name when caching an endpoint.

app.MapGet("/catalog", GetCatalog)
   .CacheOutput("Long");

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