Skip to content

Commit 8b69093

Browse files
Fix Memory Cache Integration snippet to include UseMemoryCache (#5312)
Fixes #5271 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent cd3d880 commit 8b69093

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

entity-framework/core/performance/advanced-performance-topics.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,31 @@ As with any layer, EF Core adds a bit of runtime overhead compared to coding dir
326326

327327
## Memory Cache Integration
328328

329-
EF Core integrates with ASP.NET Core's memory caching infrastructure through `IMemoryCache`. However, this is not used for the internal service provider caching.
329+
EF Core uses `IMemoryCache` for internal caching operations such as query compilation and model building. By default, EF Core configures its own `IMemoryCache` with a size limit of 10240. For reference, a compiled query has a cache size of 10, while the built model has a cache size of 100.
330330

331-
EF Core automatically configures `IMemoryCache` with a default size limit of 10240 for internal caching operations such as query compilation and model building. You should call `AddMemoryCache` if you need to change these defaults. For reference, a compiled query has a cache size of 10, while the built model has a cache size of 100.
331+
If you need to change the default cache size limit, use <xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.UseMemoryCache*> to provide a custom `IMemoryCache` instance:
332332

333333
```csharp
334-
public void ConfigureServices(IServiceCollection services)
334+
var memoryCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 20480 });
335+
services.AddSingleton<IDisposable>(memoryCache);
336+
337+
services.AddDbContext<ApplicationDbContext>(options =>
335338
{
336-
services.AddMemoryCache(options => options.SizeLimit = 20480); // Custom size limit for EF Core caching
337-
338-
services.AddDbContext<ApplicationDbContext>(options =>
339-
options.UseSqlServer(connectionString));
340-
}
339+
options.UseMemoryCache(memoryCache);
340+
options.UseSqlServer(connectionString);
341+
});
342+
```
343+
344+
The `MemoryCache` instance is registered as `IDisposable` so that it will be disposed when the service provider is disposed, without replacing the app-wide `IMemoryCache`.
345+
346+
Alternatively, if you register a custom `IMemoryCache` via `AddMemoryCache` in DI, you can resolve it from the service provider. Note that this shares the cache between EF Core and any other services that use `IMemoryCache`, so the size limit should account for all consumers. When a `SizeLimit` is set, all cache entries from every consumer must specify a size; otherwise `IMemoryCache` will throw when entries are added:
347+
348+
```csharp
349+
services.AddMemoryCache(options => options.SizeLimit = 20480);
350+
351+
services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
352+
{
353+
options.UseMemoryCache(serviceProvider.GetRequiredService<IMemoryCache>());
354+
options.UseSqlServer(connectionString);
355+
});
341356
```

0 commit comments

Comments
 (0)