Uh oh!
There was an error while loading. Please reload this page.
Add Clear() to MemoryCache - #57631
Conversation
ghost
commented
Aug 18, 2021
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
ghost
commented
Aug 18, 2021
Tagging subscribers to this area: @eerhardt, @maryamariyan, @michaelgsharp |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| { | ||
| CheckDisposed(); | ||
| // the following two operations are not atomic change as a whole, but an alternative would be to introduce a global lock for every access to _entries and _cacheSize |
There was a problem hiding this comment.
Could we get away with this by introducing a new class that contained both the dictionary and the size? Then we can atomically replace the pointer with a new instance?
There was a problem hiding this comment.
In this particular case, yes. In other, where we add/remove item from the cache and update the size afterwards, not.
runtime/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs
Lines 265 to 270 in 40cbe82
runtime/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs
Lines 151 to 158 in 40cbe82
I am not sure if it's worth it.
There was a problem hiding this comment.
In those other cases you can atomically read the pointer, store it in a local variable and modify that. If Clear gets called concurrently, it will either update the pointer before or after the other mutation. Thus, we should always have a coherent set of entries and cache size.
There was a problem hiding this comment.
@eerhardt well, it took me a while to get back to this PR. PTAL at my recent commit.
Uh oh!
There was an error while loading. Please reload this page.
- Pulling MemoryCache fixes from dotnet/runtime#57631 and dotnet/runtime#61187
- Pulling MemoryCache fixes from dotnet/runtime#57631 and dotnet/runtime#61187
eerhardt
left a comment
There was a problem hiding this comment.
Looks good to me. Thanks for the contribution here @adamsitnik.
I just had some minor comments and questions.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| private readonly ConcurrentDictionary<object, CacheEntry> _entries; | ||
| private long _cacheSize; | ||
| private CoherentState _coherentState; |
There was a problem hiding this comment.
Is this a case where we need volatile? My intuition tells me "no" because we only ever read the field once at the beginning of a method, and then use the local variable. But I wanted to ask the question, since I'm not an expert on when to use volatile.
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
fixes#45593