ASP.NET Core Web API Interview Question #136
What is in-memory caching?
Error Handling, Logging, Caching, Rate Limiting, CORS & Reliability Senior Advanced
Quick Interview Answer
In-memory caching stores data in the memory of the current application process. It is fast and simple for data that is safe to recreate and where process-local cache behavior is acceptable.
Detailed Explanation
In-memory caching stores data in the memory of the current application process. It is fast and simple for data that is safe to recreate and where process-local cache behavior is acceptable.
In a multi-instance deployment, each server has a separate memory cache. It should therefore not be treated as shared state across the cluster.
Code Example
builder.Services.AddMemoryCache();
if (!_cache.TryGetValue("categories", out List<CategoryDto>? items))
{
items = await LoadCategoriesAsync(token);
_cache.Set("categories", items, TimeSpan.FromMinutes(5));
}