dotnet / dotnet/AspNetCore.Docs
Microsoft.Extensions.Caching.Memory docs should have more details about GetOrAdd* calling the factory multiple times
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 97
Description
Currently, there is one tiny mention in "Additional Notes" at the very bottom of this page that the factory used by `GetOrCreate` and `GetOrCreateAsync` can be called multiple times.
As can be seen in [this GH issue](https://github.com/dotnet/runtime/issues/36499), and from my own experience, people often get tripped up by this fact.
Also, the examples in this document that show non-atomic use of `TryGetValue` and `Set`, such as the `CacheTryGetValueSet()` example and multiple more, are showing a bad pattern that is going to lead non-expert devs to write problematic code.
I would suggest:
- mentioning the non-locking nature of `GetOrCreate*`, more explicitly, such as in the `CacheGetOrCreate*` examples.
- mention how to handle this case (using `Lazy` to lock the delegate is the easiest and most foolproof way)
- consider revising the examples with separate GET/SET like `CacheTryGetValueSet` so that they're not antipatterns, or at least call out that they're not production code or something.
Thanks!!
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: 13f5f8f9-18dd-9c12-9ed1-fcc6b44ba7fd
* Version Independent ID: 640b4f36-5886-7b69-1c95-cfc8574a02fd
* Content: [Cache in-memory in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-5.0)
* Content Source: [aspnetcore/performance/caching/memory.md](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/caching/memory.md)
* Product: **aspnet-core**
* Technology: **aspnetcore-performance**
* GitHub Login: @Rick-Anderson
* Microsoft Alias: **riande**
Contributor guide
Assessment
This issue has not been assessed yet.