skip to content

IMemoryCache Entries

IMemoryCache keeps objects in the web process with absolute or sliding expiration, size limits and eviction callbacks. Interviewers ask why each server holds its own copy and what GetOrCreate misses.

part ofASP.NET Coreread it →
on this pageshow

explore

questions

5

In ASP.NET Core's IMemoryCache, how do absolute and sliding expiration differ, and why would you set both on one entry?

level: juniorimportance: must knowfreq 58%

answer

  1. fixed deadline vs idle timeout
  2. AbsoluteExpirationRelativeToNow vs AbsoluteExpiration
  3. each hit resets the sliding window
  4. sliding alone can live forever
  5. whichever passes first evicts

basics

~20 s

Absolute expiration removes an entry at a fixed time no matter how often it is read; sliding expiration removes it after a period with no reads. Sliding alone can keep a hot entry forever, so an absolute cap bounds staleness.

solid answer

~40 s

`MemoryCacheEntryOptions` (or the `ICacheEntry` passed to a factory) carries both. **Absolute** expiration is a deadline: `AbsoluteExpirationRelativeToNow` (a `TimeSpan` from the write, set with `SetAbsoluteExpiration(TimeSpan)`) or `AbsoluteExpiration` (a `DateTimeOffset`). **Sliding** expiration (`SetSlidingExpiration`) evicts an entry that has not been read for that long; every successful read resets the clock, but it never extends past an absolute deadline. A sliding-only entry that is read constantly never expires, which is why you add an absolute cap: the entry then goes at whichever limit comes first. With neither set there is no expiration at all. Expiry is checked lazily: a read never returns an expired entry, but removal happens on access or in a background scan that cache activity starts at most once per `ExpirationScanFrequency` (1 minute by default).

code

csharp · 6 lines
csharp
var corridor = await cache.GetOrCreateAsync($"corridor:{from}-{to}", async entry =>
{
    entry.SetSlidingExpiration(TimeSpan.FromMinutes(10));
    entry.SetAbsoluteExpiration(TimeSpan.FromHours(6));
    return await corridors.LoadAsync(from, to);
});

go deeper

for a junior

Recall the two settings: absolute is a fixed deadline, sliding is an idle timeout that each read resets. Say why sliding alone can keep a hot entry forever.

for a middle

Explain the option names and helpers, that the earlier limit wins when both are set, that zero spans throw ArgumentOutOfRangeException, and that expiry is checked on access and by a periodic scan.

for a senior

Choose the limit from the staleness the business accepts, not from popularity, and explain why expired entries can linger and why eviction callbacks for time-based expiry fire late.

for a principal

Weigh how expiry per instance interacts with a farm: unsynchronised clocks across servers, cold caches after deploys, and when a bounded-staleness rule needs explicit invalidation instead of time.

## Two different clocks An `IMemoryCache` entry can carry two independent time limits, both set through `MemoryCacheEntryOptions` (or directly on the `ICacheEntry` a `GetOrCreate` factory receives): | Setting | Helper | Meaning | Reset by reads? | |---|---|---|---| | `AbsoluteExpirationRelativeToNow` | `SetAbsoluteExpiration(TimeSpan)` | expires this long after the entry is written | no | | `AbsoluteExpiration` | `SetAbsoluteExpiration(DateTimeOffset)` | expires at this instant | no | | `SlidingExpiration` | `SetSlidingExpiration(TimeSpan)` | expires after this long without a read | yes, every hit | The `Set(key, value, TimeSpan)` overload is shorthand for a relative absolute expiration, and `Set(key, value, DateTimeOffset)` for a fixed instant. If both a relative and a fixed absolute expiration are given, the cache keeps whichever deadline is earlier. Zero or negative spans are rejected with `ArgumentOutOfRangeException` ("The relative expiration value must be positive." / "The sliding expiration value must be positive."). ## How sliding expiration really behaves Each successful `TryGetValue` stamps the entry's last-access time. When the cache next checks the entry, it is expired if the absolute deadline has passed **or** if the time since the last access is at least the sliding window. Two properties follow: - **A sliding window never outlives the absolute deadline.** The absolute check runs first; reads cannot push an entry past it. - **A sliding-only entry can live forever.** If something reads it more often than the window, it is never idle long enough to expire. The ASP.NET Core caching docs call this out directly and recommend combining sliding with absolute expiration. ## Picking them for exchange rates In a remittance service the provider publishes new rates every 30 seconds, and a quoted rate must never be older than that: ```csharp var options = new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromSeconds(30)); cache.Set($"fx:{from}:{to}", rate, options); var corridorOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(10)) .SetAbsoluteExpiration(TimeSpan.FromHours(6)); cache.Set($"corridor:{from}-{to}", corridor, corridorOptions); ``` - **Rates** get an absolute limit only: the staleness bound is the business rule, and popularity must not stretch it. - **Corridor metadata** (fees, limits, payout partners) changes rarely and many corridors are seldom used, so a sliding window lets cold corridors drop out while the absolute cap still forces a periodic reload of busy ones. ## When "expired" is noticed `MemoryCache` has **no timer** that removes entries the moment their time is up. Expiration is evaluated: 1. **On access.** `TryGetValue` checks the entry; an expired entry is removed and reported as a miss, so a caller never receives it. 2. **In a background scan.** Any cache operation (`TryGetValue`, `Set`, `Remove`) starts a scan on the thread pool if more than `MemoryCacheOptions.ExpirationScanFrequency` has elapsed since the last one; the default is **1 minute**. So correctness is unaffected - an expired value is never served - but an expired entry can occupy memory until the next access or scan, and an eviction callback registered on it runs only when that removal happens. Token-based expiration (an `IChangeToken` added with `AddExpirationToken`) is different: a token that supports change callbacks queues the entry's removal as soon as it fires. ## Choosing per kind of data A short checklist decides the options for each kind of entry: 1. **Is there a maximum age the business accepts?** Then set an absolute expiration equal to it. Rates, prices and anything quoted to a customer belong here. 2. **Is the data large, numerous and unevenly used?** Add a sliding window so rarely read keys leave memory early. 3. **Is it reference data that changes only on deploy?** An absolute expiration of hours, or an expiration token signalled when the data changes, is enough. 4. **Is it unsafe to serve at all after a write?** Time is the wrong tool; remove or token-evict the entry on the write path. The answer should come from how stale the value may be, not from how often it is read. ## The traps - **No expiration is not a short expiration.** An entry written without any option stays until it is removed, replaced or compacted. - **Sliding expiration does not bound staleness.** It bounds idleness. A value read every second under a five-minute sliding window can be days old. - **`DateTimeOffset` deadlines are instants.** `SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddMinutes(5))` written once into a shared options object gives every later entry the same instant, which may already be in the past; reuse a `TimeSpan` instead. - **Expiry is per process.** Each server instance starts its clocks when it writes its own copy, so instances do not expire in step.

  • If a rate entry expired 20 seconds ago and nobody has touched the cache since, is it still in memory?
    Yes. `MemoryCache` has no timer; expiry is evaluated when the entry is accessed or during a scan that a cache operation starts at most once per `ExpirationScanFrequency` (one minute by default). The next `TryGetValue` for that key removes it and reports a miss, so the stale rate is never returned.
  • Why is reusing one MemoryCacheEntryOptions with SetAbsoluteExpiration(DateTimeOffset) risky?
    A `DateTimeOffset` is a fixed instant computed once. Every entry written later with the same options object shares that instant, so entries written after it has passed are already expired. A `TimeSpan` passed to `SetAbsoluteExpiration` is applied relative to each write instead.
  • What does ExpirationScanFrequency change, and should you lower it?
    It is the minimum gap between background scans for expired entries, one minute by default. Lowering it frees memory held by expired entries sooner and makes time-based eviction callbacks fire closer to the deadline, at the cost of more full scans of the dictionary. It never affects whether an expired value is served.

  • Sliding expiration guarantees a cached value is never older than the sliding window
  • A background timer removes each entry at the exact moment it expires
  • Entries without any expiration option expire after a built-in default of a few minutes
  • Each read of an absolute-expiration entry pushes its deadline further out
  • A sliding window can keep an entry alive past its absolute expiration
open as a page

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%

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.

open as a page

In ASP.NET Core's IMemoryCache, what do RegisterPostEvictionCallback and AddExpirationToken do, and when does an eviction callback actually run?

level: middleimportance: should knowfreq 32%

basics

~20 s

RegisterPostEvictionCallback runs a delegate on the thread pool after an entry leaves the cache, with an EvictionReason. AddExpirationToken ties the entry to an IChangeToken, such as a CancellationChangeToken, whose firing queues its eviction at once.

open as a page

When a cached exchange rate expires and 200 concurrent requests call IMemoryCache.GetOrCreateAsync for it in ASP.NET Core, how often does the factory run, and why?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Possibly up to 200 times. GetOrCreateAsync is an extension that calls TryGetValue, then awaits the factory and commits the entry, with no lock per key; every caller that misses before the first commit runs its own factory.

open as a page

A remittance service caps its exchange-rate IMemoryCache with SizeLimit; what must every entry then carry, and what happens when the limit is reached?

level: seniorimportance: should knowfreq 34%

basics

~10 s

Once MemoryCacheOptions.SizeLimit is set, every entry needs a Size or committing it throws InvalidOperationException. A write that would exceed the limit is not stored, and a background compaction evicts expired, then low-priority, least-recently-used entries.

open as a page