FusionCache: Fail-safe getting or setting featuring a default value
Hi, if the cache is initally empty, even a fail-safe GetOrSet still throws an exception, since there is no expired value for the key which can be retrieved. There is a GetOrDefault, but from the otherwise excellent documentation and the example code alone it is not clear, how to combine these features. Is that really some kind of missing method (like a GetOrSetOrDefault feature or an overload with a default value) or is this by design? Either way, it would be awesome if this case is mentioned. Thank you very much!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 17 (3 by maintainers)
This is awesome!
Also, regarding the documentation: here is a new dedicated page about core methods.
What do you think @clxx ?
I’ve release v0.1.3 containing this very feature 🎉
This sounds great!
I really like the idea of using such a
MaybeValue<TValue>here…First of all, thank you, since this makes it much clearer. For me, this explanation is perfect - you may only add one half sentence about the new
failSafeDefaultValuebeing “returned whenever anything goes wrong with the factory execution” and there is no usable fallback entry anymore (initally or due to final expiration).I think the “negative cache” concept at the moment is something to think about. It is causing me some friction for a few use cases in an existing project where I have a bunch of unit tests asserting the “negative cache” behavior. As I replace with FusionCache I am not 100% sold on “negative-cache” being a feature request. With all the features in FusionCache I just need to keep using it for a while to decide if I need that “negative cache” feature. I have been in this code base for a solid week and experimenting with it in a project. I just need to let all this marinate for some time.
@jodydonetti thanks for sharing this project with us!
I will update my metrics PR to this code and try to get some experimenting time with this change.
Hi @clxx , I’ve just pushed a couple of new features.
Added param
failSafeDefaultValuetoGetOrSet[Async]In the commit 1900d960090d69382ed06688270d0d4e9fd6438b I added a new
failSafeDefaultValueparam to the coreGetOrSetmethod. This is a way to specify an optional default value to use when fail-safe is enabled and something goes wrong with the factory execution (a timeout, an exception, or anything else).The type of the param is the new
MaybeValue<TValue>which is logically similar to aNullable<T>, but which can be used with any type including reference types (somewhat like the maybe/option monad, if you are into functional programming): since it is a struct it does not allocate on the heap, so less heap usage when not used. It also implements implicit conversion to/fromTValueso you don’t have to be explicit when using it.Of course I added all the needed overloads to be able to work with
TimeSpaninstead of explicit options etc, and also the ones needed to map all the old methods signatures (without the new param) and related overloads, so everything should - in theory 😅 - compile just like before without any change.Added a new
GetOrSet[Async]method with a direct value (instead of a factory)Kinda related, some people asked me how to get a value from the cache and, if not there, set a value directly without providing a factory, without the useless lambda allocation and with a less cumbersome way to call it. So in f7c648033f53630ccbf9fa1b7442303859c2265f I added such new core method (and related overloads for
TimeSpan, etc).Practical example
Before, if we had a value to use when a cache entry was missing, we had to do this:
Now instead we can do this:
Documentation
Initially you (@clxx ) said this:
I thought about this a lot and I think that, on top of adding the new
failSafeDefaultValueparam to actually combine factory + default value, I should better clarify in the documentation the difference betweenGetOrSetandGetOrDefault. I was thinking about something like this:What would you think of an explanation like that?
Closing thoughts
Before publishing a new package version on nuget.org I would like to know what you all think about it ( @clxx @JoeShook @alb-xss ). Do you see any problems? Do you think the
MaybeValue<TValue>is a good or bad idea?Hi all, I think I got the right design now.
I’m doing some more tests and perf tuning, and in the next few days I’ll push the new feature so you can play with it and give me a feedback.
Wow, lots of food for thoughts, thanks!
Let me marinate these ideas, maybe sleep over them and the possible consequences a little more, and I’ll come back to you with something.