alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Caching Booleans always return False #93

Closed robdplatt closed 4 years ago

robdplatt commented 5 years ago

Anything having trouble caching booleans?

Take for example:

        Dim testBool As Boolean = cache.GetOrAdd("key", Function()
                                                                             Return True
                                                                         End Function)

This will return False.

However,

        Dim testBool As Boolean = cache.GetOrAdd(of string)("key", Function()
                                                                             Return True
                                                                         End Function)

This will return True.

alastairtree commented 4 years ago

Thanks for this, could you submit a PR with a failing unit test for this (in c#)?

robdplatt commented 4 years ago

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.

robdplatt commented 4 years ago

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.

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.

alastairtree commented 4 years ago

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 .

robdplatt commented 4 years ago

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.

alastairtree commented 4 years ago

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.