Open blastrock opened 3 months ago
@blastrock I think what you are looking for would only be possible using some sort of distributed lock mechanism like RedLock
when using Redis
.
However, I do not think that is within the scope of HybridCache
.
This is something that has been discussed, but it is not going to be implemented in the initial release. It is, however, something that we're happy to consider on the list for future versions. It will probably need a new optional L2 API to support it - the existing IDistributedCache
backend simply doesn't have the concept of distributed locks, or an atomic "set if not exists and tell me whether it succeeded" (which is the minimum needed for such)
Thank you for the answers, we ended up using RedLock.
In the meantime, maybe the documentation should be updated to reflect this limitation. In our use-case, we really don't want to make requests twice, and the documentation suggested HybridCache was the right tool for the job.
Is there an existing issue for this?
Describe the bug
The HybridCache documentation says:
But this mechanism seems to only rely on a local memory cache. This property isn't ensured if multiple instances of a service try to create the same key.
Expected Behavior
If multiple services try to create the same key, I expect the property to still hold true. The second service should wait for the first one to complete instead of retrying to create it.
Steps To Reproduce
Here is a simple program that will create a key (given as an argument) on a redis cache: https://github.com/blastrock/hybridcacheissue/blob/master/Program.cs
dotnet run test1
, it will print "Will get" and "Creating" and blockdotnet run test1
, it will print the same thing.I expect the second run to not call the creation delegate and wait for the first one to complete.
Exceptions (if any)
No response
.NET Version
8.0.300
Anything else?