Closed robdplatt closed 4 years ago
Thanks for this, could you submit a PR with a failing unit test for this (in c#)?
Thanks for this, could you submit a PR with a failing unit test for this (in c#)?
Yup. I'll try and have it up in the morning.
So, my apologies, this was an oversight in my implementation. I wrap LazyCache that way I can add additional features.
When I call GetOrAdd, I first check to see if the object exists in the cache by calling cache.Get
The problem is there. If my type is Boolean and it's not in the cache, it returns False. Implying the object is in the cache and the value is False.
It looks like the right way to test if my non-nullable booleans are cached is by calling cache.Get<Boolean?>(key). I can always cast the stored value back to Boolean.
Why do you check if it is in the cache? Just use GetOrAdd and then it will always return the cached version or the new version and you don't need to check?
On Fri, 6 Dec 2019, 16:47 Rob, notifications@github.com wrote:
So, my apologies, this was an oversight in my implementation. I wrap LazyCache that way I can add additional features.
When I call GetOrAdd, I first check to see if the object exists in the cache by calling cache.Get(key).
The problem is there. If my type is Boolean and it's not in the cache, it returns False. Implying the object is in the cache and the value is False.
I just realized by passing a nullable boolean it will return null if the object is not in the cache.
My problem is my Booleans are not currently nullable.
How can I test to see if my non-nullable boolean is in the cache?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alastairtree/LazyCache/issues/93?email_source=notifications&email_token=ABP3TFOUQ4QSFVTPW6VA7QTQXJ63VA5CNFSM4I5GOYZ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGEVSOY#issuecomment-562649403, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABP3TFKXJKFOINSHXNQCHHLQXJ63VANCNFSM4I5GOYZQ .
Because I've added a FileCache that runs independent of LazyCache.
I check to see if it's in the memory cache first before I check the file cache. If it's not and I use GetOrAdd then I won't know if it was previously already in cached. If it's not, it won't check the file cache.
Hope that makes sense.
I think the issue here is that the default type for a bool is false - so if you miss the cache (say it is expired) you would still get false back. I have added this toi a new troubleshooting docs page, but as you noticed the solution is to always cache a nullable type like bool?
instead.
Will close this issue now as we have updated docs and a solution. Thanks.
Anything having trouble caching booleans?
Take for example:
This will return False.
However,
This will return True.