dotnet / dotnet/AspNetCore.Docs

Microsoft.Extensions.Caching.Memory.MemoryCache is thread-safe

Open
#33,103 0 comments 0 reactions 1 assignee Assigned to @tdykstra View on GitHub
doc-enhancement Pri2 seQUESTered Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### Description

Email from Feng Yuan:

>The documentation is just too simple.

>Microsoft.Extensions.Caching.Memory.MemoryCache is implemented using ConcurrentDictionary (not Dictionary), thus thread-safe. The documentation should mention that is Count property is super expensive, so avoid calling it too often.

>System.Runtime.Caching.MemoryCache is implemented using an array of Hashtable, which is designed to be thread-safe for single writer/multiple readers. To make write operations thread-safe, lock is taken internally.

>The number of locks in Microsoft.Extensions.Caching.Memory.MemoryCache goes from core count to 1,024.

>The number of locks in System.Runtime.Caching.MemoryCache is just core count.

>So the new implementation could be better with high concurrent write operations.

### Page URL

https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-8.0

### Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/caching/memory.md

### Document ID

640b4f36-5886-7b69-1c95-cfc8574a02fd

### Article author

@tdykstra

---
[Associated WorkItem - 302697](https://dev.azure.com/msft-skilling/Content/_workitems/edit/302697)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.