C# Interview Question #189
What is caching, and what caching options exist in ASP.NET Core?
Testing, Security, APIs & Reliability Senior Advanced
Quick Interview Answer
Caching stores reusable data or responses so expensive work does not need to be repeated for every request.
Detailed Explanation
Caching stores reusable data or responses so expensive work does not need to be repeated for every request.
ASP.NET Core applications can use IMemoryCache for in-process caching, distributed caches for multi-instance deployments, output caching for HTTP responses, and external systems such as Redis.
A correct caching strategy must define expiration, invalidation, consistency requirements, cache keys, memory limits, and behavior when the cache is unavailable.
Caching should target measured expensive and reusable operations rather than being added indiscriminately.
Code Example
builder.Services.AddMemoryCache();
public class ProductService
{
private readonly IMemoryCache _cache;
public ProductService(IMemoryCache cache)
{
_cache = cache;
}
}