skip to content

Does EF Core 10 have a built-in second-level cache, and how would you cache a hot product search's results instead?

level: seniorimportance: should knowfreq 34%

answer

  1. the change tracker dies with the context
  2. plans cached, rows never
  3. SizeLimit 10240 in EF's own cache
  4. cache the projected DTOs
  5. SuppressWithResult in ReaderExecutingAsync

basics

~20 s

No. EF Core caches only per-context tracked entities and process-wide query plans and models, never result rows across requests. Caching a hot search means caching its projected DTOs in the application, or replacing the reader through a command interceptor.

solid answer

~40 s

EF Core has no second-level cache. The change tracker is a first-level identity map that lives and dies with one context, which in ASP.NET Core means one request. The process-wide cache EF does keep, an internal `IMemoryCache` with a `SizeLimit` of 10240, holds compiled queries and models, not rows, so every execution still reaches the database. To cache a hot search, cache its projected `ProductCard` list in the application layer, keyed by the search parameters and invalidated when products change. The lower-level alternative is a command interceptor whose `ReaderExecutingAsync` returns `InterceptionResult<DbDataReader>.SuppressWithResult(...)` with a cached reader, often keyed on a `TagWith` marker; community packages wrap that idea, and their invalidation rules are what to scrutinize.

code

csharp · 19 lines
csharp
public sealed class SearchCacheInterceptor(SearchResultStore store) : DbCommandInterceptor
{
    public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
        DbCommand command,
        CommandEventData eventData,
        InterceptionResult<DbDataReader> result,
        CancellationToken cancellationToken = default)
    {
        if (command.CommandText.StartsWith("-- product search", StringComparison.Ordinal)
            && store.TryGetReader(command, out var cached))
        {
            result = InterceptionResult<DbDataReader>.SuppressWithResult(cached);
        }

        return ValueTask.FromResult(result);
    }
}

// registration: options.AddInterceptors(new SearchCacheInterceptor(store));

go deeper

for a junior

Recall that EF Core has no second-level cache and that a scoped context's change tracker is discarded at the end of each request.

for a middle

Explain the difference between EF's internal query-plan cache and a result cache, and why DTOs are the safer thing to cache.

for a senior

Show how a hot search would be cached and invalidated, and what an interceptor-based cache must get right about thread safety and stale data.

for a principal

Decide which data tolerates staleness, who owns invalidation across services, and whether caching or a faster query is the better investment.

## What "second-level cache" means here In ORM vocabulary the **first level** is the per-session identity map, and a **second-level cache** is a process-wide or distributed store that answers repeated reads without querying the database. EF Core ships the first and not the second. ## What EF Core caches by itself | Cache | Scope | Holds | Serves a repeated search? | |---|---|---|---| | Change tracker (identity map) | one `DbContext` instance | tracked entities by key | no; a tracking query still runs, though `Find` can answer from it | | Internal `IMemoryCache` | the process | compiled queries (size 10 each) and built models (size 100), `SizeLimit` 10240 | no; it saves translation, not the round trip | | Plan reuse in the database, where the engine keeps one | the server | execution plans | no; the database still executes the query | In an ASP.NET Core app the context is normally **scoped**, so the change tracker disappears when the request ends. `UseMemoryCache` lets you supply EF's internal cache instance, for example to change its size, but it still stores plans and models only. `Find` and `FindAsync` return an already tracked entity without a query, but only from the same context instance. ## Option 1: cache the result in the application The usual answer for a hot, read-heavy search is to cache what the endpoint returns, not what EF materializes: 1. Run the projected query: `Select` into `ProductCard`, no tracking involved. 2. Store the list under a key built from the search inputs, such as category, sort and page. 3. Give the entry a short lifetime, and tag or evict it when a product in that category changes. Caching DTOs avoids the traps of caching entities: no detached instances that look tracked, no navigation graphs pinned in memory, and nothing tied to a disposed context. The app's caching layer, such as `HybridCache.GetOrCreateAsync` with tag-based removal, is its own topic; from EF's side, the point is that the cached value is a projection. ## Option 2: intercept the command EF Core's **interceptors** can stop a command from reaching the database. A class deriving from `DbCommandInterceptor` overrides: - `ReaderExecutingAsync`: if the command text starts with a known tag, such as `-- product search`, and a fresh cached result exists, return `InterceptionResult<DbDataReader>.SuppressWithResult(cachedReader)` and EF reads from that reader instead; - `ReaderExecutedAsync`: after a real execution, copy the rows from the real reader into the cache for next time. The interceptor is registered with `AddInterceptors` and, because it outlives contexts, must be thread-safe. The EF Core docs present this as a primitive second-level cache and warn that it demonstrates the concept rather than a robust implementation; community extensions listed in the docs build full caches on the same hook. ## Building the cache key Whatever layer caches the search, the key must contain **every input that changes the result**: - the search inputs: category, text, filters, sort and page; - anything the query reads implicitly, such as the current tenant or the culture used for prices; - a version or tag for the data, so a product change can evict every key that might contain it. Missing one input is worse than having no cache: users see each other's results or a stale page that never expires. ## Why invalidation is the real cost - `SaveChanges` from another context, `ExecuteUpdateAsync`, raw SQL and other applications all change rows behind the cache's back. - A command-level cache must know which tables a command reads to evict correctly; a tag-based scheme only knows the tags you wrote. - Entities rebuilt from a cached reader are fresh instances; with tracking queries they enter the change tracker like database rows, which can mislead code that saves them. ## Choosing - Start with projection and indexes; a fast query may not need a cache. - Cache DTOs at the application layer when the data tolerates seconds or minutes of staleness, as a catalogue usually does. - Reach for an interceptor-based cache only when many queries need it uniformly and you can define invalidation by table.

  • Does calling UseMemoryCache on DbContextOptionsBuilder give EF Core a result cache?
    No. `UseMemoryCache` replaces the `IMemoryCache` EF uses internally for compiled queries and models, for example to raise its 10240 size limit or share an instance. Rows are never stored there, so every query execution still goes to the database.
  • Why cache ProductCard DTOs rather than tracked Product entities?
    A cached entity outlives the context that loaded it, so it is detached, may hold a graph of navigations in memory, and can be attached or saved by mistake later. A DTO is an immutable read model of exactly what the page needs, small to store and safe to share between requests.

EF Core's own query cache is a kitchen that keeps its recipe cards: the second order of the same dish is prepared faster, but it is still cooked from scratch. A second-level cache is a warming shelf of finished plates, and someone must throw them out when the menu changes.

  • EF Core's change tracker is a second-level cache shared by every request.
  • UseMemoryCache turns on query-result caching in EF Core.
  • A compiled query caches the rows it returned last time.
  • AsNoTracking queries are answered from EF Core's cache after the first call.
  • A hand-written command-interceptor cache is evicted automatically when SaveChanges writes the table.