ASP.NET Core Interview Question #179

What is IMemoryCache?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

IMemoryCache provides an in-process cache stored in the application's memory. It is fast and simple because no network call is required to access cached values.

Detailed Explanation

IMemoryCache provides an in-process cache stored in the application's memory. It is fast and simple because no network call is required to access cached values.

The cache belongs to one application process. In a multi-instance deployment, each instance has its own memory cache, so values are not automatically synchronized. It is best for data that can be independently cached per instance and safely rebuilt if the process restarts.

Code Example

builder.Services.AddMemoryCache();

public class CategoryService
{
    private readonly IMemoryCache _cache;

    public CategoryService(IMemoryCache cache)
    {
        _cache = cache;
    }
}