-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GetOrCreateExclusive() and GetOrCreateExclusiveAsync(): Exclusive versions of GetOrCreate() and GetOrCreateAsync() #36500
Comments
In the async version it should of cause be |
Looking at the MemoryCache.cs implementation, I wonder how bad would it be to use ConcurrentDictionary there instead of plain Dictionary. and expose the GetOrAdd method directly. I know that the interface doesn't have it and I would really hate IMemoryCache2, but at this point, I'm seriously considering forking implementation of the MemoryCache nd making change myself... |
@ttrider We are looking into updating the implementation of |
Hello John, This is the great news! While we wait on your implementation, for a time being, we created the following poor-man wrapper implementation. https://gist.github.com/ttrider/a5ae8fc86ccfe6a4243f4481a5858a80 I think I’ll rename my methods to match your signatures – it will make dropping and replacing them easier. Thank You, Vladimir |
I don't think this can fit into 2.0 right now. It needs a fairly large amount of investigation and testing to make it work well. |
It would be great if in the async version, expiry could be set as a function of the retrieved value (see https://github.com/aspnet/Caching/issues/338). |
@JunTaoLuo |
2 proposed solutions: Updated: added consistent exceptions propagation |
@jelical welldone, thanks. Could you please share that your test code? |
@jelical Considering that As far as I can see, using |
Greetings, Here's my solution for your issue. It's based on one It uses more abstracted |
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label. |
Related StackOverflow question: Stop Reentrancy on MemoryCache Calls |
For completeness. If I understand correctly, the following is proposed here: namespace Microsoft.Extensions.Caching.Memory {
public static partial class CacheExtensions {
public static TItem GetOrCreateExclusive<TItem>(this IMemoryCache<TItem>, object key, Func<ICacheEntry, TItem> factory);
public static Task<TItem> GetOrCreateExclusiveAsync<TItem>(this IMemoryCache<TItem>, object key, Func<ICacheEntry, Task<TItem>> factory)
}
} An implementation example has already been given by @rmja in top post. I myself have not worked much with |
I don't think we would want these extension methods added built-in to the library. Doesn't seem like a bigger value add than just letting whomever needs this functionality to just add it directly into their code. Would make sense to keep the onus on the app side to make sure they keep their transactions thread safe by also assuring they don't make calls to Closing as won't fix. |
It would be nice with equivalents of
GetOrCreate()
andGetOrCreateAsync()
where it is ensured that the factory is only invoked once for each cache miss.This basically resolves aspnet/Caching#218, but with alternate extension methods.
An implementation proposal:
The text was updated successfully, but these errors were encountered: