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, 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
Currently, there is one tiny mention in "Additional Notes" at the very bottom of this page that the factory used by
GetOrCreate
andGetOrCreateAsync
can be called multiple times.As can be seen in this GH issue, 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
andSet
, such as theCacheTryGetValueSet()
example and multiple more, are showing a bad pattern that is going to lead non-expert devs to write problematic code.I would suggest:
GetOrCreate*
, more explicitly, such as in theCacheGetOrCreate*
examples.Lazy
to lock the delegate is the easiest and most foolproof way)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.