skip to content

In ASP.NET Core, how do you register IMemoryCache and read or write an entry with TryGetValue and Set, and where does that data live?

level: juniorimportance: must knowfreq 64%

answer

  1. one shared instance per process
  2. AddMemoryCache uses TryAdd
  3. TryGetValue<T> returns bool, out T?
  4. Set<T> returns the value it stored
  5. object reference, no serialization

basics

~20 s

Call builder.Services.AddMemoryCache(), inject IMemoryCache, read with TryGetValue (a bool plus an out value) and write with Set. Entries are object references in that server process's heap: every instance keeps its own copy, lost on restart.

solid answer

~40 s

`builder.Services.AddMemoryCache()` registers one `MemoryCache` behind `IMemoryCache` (with `TryAdd`, so a second call, or the `TryAddSingleton` that MVC's Razor services make, does not duplicate it). You inject `IMemoryCache` and use the `CacheExtensions` helpers: `TryGetValue<TItem>(key, out TItem? value)` returns `true` only when the key exists and the stored object is a `TItem` (or `null`), and `Set<TItem>(key, value, ...)` stores the value and returns it, with overloads for an absolute `DateTimeOffset`, a relative `TimeSpan`, an `IChangeToken` or a `MemoryCacheEntryOptions`. `Remove(key)` deletes an entry. The cache keeps the **object reference** in the process heap: nothing is serialized, so mutating a returned object mutates the cached one. Each server instance has its own independent copy that disappears when the process restarts, and an entry with no expiration stays until it is removed or compacted.

code

csharp · 19 lines
csharp
using Microsoft.Extensions.Caching.Memory;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<IRateFeed, RateFeed>();
var app = builder.Build();

app.MapGet("/rates/{from}/{to}", async (string from, string to, IMemoryCache cache, IRateFeed feed) =>
{
    var key = $"fx:{from}:{to}";
    if (!cache.TryGetValue(key, out decimal rate))
    {
        rate = await feed.FetchAsync(from, to);
        cache.Set(key, rate, TimeSpan.FromSeconds(30));
    }
    return TypedResults.Ok(new { from, to, rate });
});

app.Run();

go deeper

for a junior

Recall AddMemoryCache, injecting IMemoryCache, TryGetValue returning a bool with an out value, and Set with an expiration. Say plainly that the data lives inside one server process.

for a middle

Explain that the helpers are extension methods on CacheExtensions, that a type mismatch makes TryGetValue return false, that Set replaces an entry, and that no expiration means the entry stays until removed or compacted.

for a senior

Show the production consequences: object references shared across requests demand immutable values, every instance has its own copy and its own expiry clock, and nothing bounds memory unless you set a size limit.

for a principal

Frame the choice of tier: what staleness between instances the business tolerates, what a cold cache after every deploy costs the downstream system, and when that justifies an out-of-process store.

## What IMemoryCache is `IMemoryCache` (namespace `Microsoft.Extensions.Caching.Memory`) is the in-process cache that ships with ASP.NET Core's shared framework. Its only implementation in the box is `MemoryCache`, which keeps entries in a concurrent dictionary inside the web process. The interface itself is tiny: `TryGetValue(object key, out object? value)`, `CreateEntry(object key)`, `Remove(object key)` and a default `GetCurrentStatistics()` that returns `null` unless the implementation tracks statistics. Everything else an application calls - `Get<T>`, `TryGetValue<T>`, `Set<T>`, `GetOrCreate`, `GetOrCreateAsync` - is an extension method on the static `CacheExtensions` class. ## Registering it ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddMemoryCache(); ``` `AddMemoryCache()` adds the options infrastructure and registers `MemoryCache` as the single `IMemoryCache` for the container with `TryAdd`. Two consequences matter in practice: - **One instance per process.** Every class that injects `IMemoryCache` shares the same dictionary, so keys from unrelated features can collide; prefix them (`fx:USD:MXN`, `corridor:US-MX`). - **Calling it twice is harmless.** The `TryAdd` means a second call, or a framework component that registered `IMemoryCache` first, does not create a second cache. The overload `AddMemoryCache(Action<MemoryCacheOptions>)` configures that one shared instance. ## Reading and writing In a remittance service that converts a sender's amount at the current rate: ```csharp public sealed class RateQuotes(IMemoryCache cache, IRateFeed feed) { public async Task<decimal> GetRateAsync(string from, string to) { var key = $"fx:{from}:{to}"; if (cache.TryGetValue(key, out decimal rate)) return rate; rate = await feed.FetchAsync(from, to); return cache.Set(key, rate, TimeSpan.FromSeconds(30)); } } ``` | Call | Returns | Notes | |---|---|---| | `TryGetValue<TItem>(key, out TItem? value)` | `bool` | `true` when found and the stored object is a `TItem` or `null`; a value of another type gives `false`, not an exception | | `Get<TItem>(key)` | `TItem?` | `default` on a miss | | `Set<TItem>(key, value)` | the value | no expiration at all | | `Set<TItem>(key, value, TimeSpan)` | the value | expires that long after the write | | `Set<TItem>(key, value, DateTimeOffset)` | the value | expires at that instant | | `Set<TItem>(key, value, MemoryCacheEntryOptions)` | the value | expiration, priority, size, tokens, callbacks | | `Remove(key)` | `void` | the entry is evicted with reason `Removed` | A miss never throws: `TryGetValue` simply returns `false`. `Set` replaces an existing entry for the same key, and the old one is evicted with reason `Replaced`. ## Where the data lives The cache stores **the object reference you passed**, in the managed heap of the process that called `Set`. That has three effects interviewers probe: 1. **No serialization, no copy.** Reading a cached `List<RateSnapshot>` and adding to it changes the cached list for every later reader. Cache immutable objects (records, `ImmutableArray<T>`) or copy before mutating. 2. **One copy per server.** Three instances behind a load balancer hold three independent caches, each filled by its own misses and each expiring on its own clock, so two requests for the same corridor can briefly see different rates. Sharing one copy needs an out-of-process store (`IDistributedCache`) or the two-tier `HybridCache`; keeping per-instance copies in agreement is a separate design problem. 3. **Gone on restart.** A deploy, a crash or an app-pool recycle empties it; the first requests afterwards all miss. ## What it does not do for you - **No default expiration.** An entry written with plain `Set(key, value)` stays until it is removed, replaced or compacted, or until the process ends. - **No memory-pressure trimming.** The cache has no size limit by default (`MemoryCacheOptions.SizeLimit` is `null`) and the docs state plainly that the runtime does not trim it when memory runs low; bounding it is the developer's job. - **No locking around a miss.** Two requests that miss at the same time both fetch and both `Set`; the second write simply replaces the first. ## Keys deserve a convention Keys are compared with the key object's own equality, so any object works, but strings are the norm. Because one instance serves the whole process, a key such as `"USD:MXN"` written by the rate feature and read by a fee feature that stores something else under the same text produces exactly the type-mismatch miss described above. A short prefix per feature (`fx:`, `corridor:`, `fee:`) and a single place that builds each key avoid it. ## How to say it in an interview Register with `AddMemoryCache()`, inject `IMemoryCache`, read with `TryGetValue` and write with `Set` plus an expiration. Then add the two facts that separate a working answer from a weak one: the cache holds live object references in one process, so values must be treated as immutable, and every server instance has its own copy that a restart wipes.

  • Why does TryGetValue<decimal> return false for a key that exists?
    The extension calls the non-generic `TryGetValue` and then checks `result is TItem`. If another feature stored a `string` or a boxed `double` under the same key, the type test fails and the method returns `false` with `default` in the out parameter. It does not throw `InvalidCastException`. A stored `null` is the one case that returns `true` for any type.
  • A colleague caches a mutable List<RateSnapshot> and later appends to the list a request read; what goes wrong?
    `IMemoryCache` stores the reference, not a copy, so the append changes the cached list for every concurrent and later reader, and `List<T>` is not safe for concurrent writes. Cache immutable data such as records or `ImmutableArray<T>`, or copy the list before changing it.
  • When would you choose IMemoryCache over a distributed cache?
    When the data is cheap to rebuild, tolerates each instance holding a slightly different copy for the length of its expiration, and is read often enough that a network hop would dominate. Reference data, rates with a short expiration and computed lookups fit. Data every instance must see identically, or that must survive a restart, needs an out-of-process store.

  • IMemoryCache entries are shared across every instance of the app behind the load balancer
  • Set copies the value, so changing the object afterwards cannot affect the cache
  • Entries expire after some built-in default timeout even when no expiration is set
  • TryGetValue throws KeyNotFoundException when the key is missing
  • The runtime trims IMemoryCache automatically when the server runs low on memory