ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
1.78k stars 92 forks source link

NullReferenceException, seems to be storing no value for bool? #323

Closed jimmycartrette closed 3 hours ago

jimmycartrette commented 3 hours ago

I'm just getting started with plugging FusionCache to replace our existing cache systems. I'm calling this: _cache.Set(GetKeyName(key), obj, expiresIn); With obj being a bool. When it's brought back: var maybeValue = _cache.TryGet(GetKeyName(key)); Of type bool, I get a nullreferenceexception.

System.Exception: Error in Evaluate_ExpiredSecretPasswordChangeMessage: System.NullReferenceException: Object reference not set to an instance of an object.
   at ZiggyCreatures.Caching.Fusion.Internals.Memory.FusionCacheMemoryEntry`1.GetValue[TValue1]() in D:\repos\FusionCache\src\ZiggyCreatures.FusionCache\Internals\Memory\FusionCacheMemoryEntry.cs:line 66
   at ZiggyCreatures.Caching.Fusion.FusionCache.GetValueFromMemoryEntry[TValue](String operationId, String key, IFusionCacheMemoryEntry entry, FusionCacheEntryOptions options) in D:\repos\FusionCache\src\ZiggyCreatures.FusionCache\FusionCache.cs:line 967
   at ZiggyCreatures.Caching.Fusion.FusionCache.TryGet[TValue](String key, FusionCacheEntryOptions options, CancellationToken token) in D:\repos\FusionCache\src\ZiggyCreatures.FusionCache\FusionCache_Sync.cs:line 565

Trying to dig into the cache itself, it has no "v" or value:

hgetall v0:Fusion:fe08c382-ef5d-4fce-87f5-670b32d0770a:SecretMessage_Enabled 1) "absexp" 2) "638656609480003673" 3) "data" 4) "{\"m\":{\"e\":\"2024-10-27T21:22:28.0003673+00:00\"},\"t\":638656573480003673}" 5) "sldexp" 6) "-1"

A good entry looks like this: 1) "absexp" 2) "638656608619653733" 3) "data" 4) "{\"v\":true,\"m\":{\"e\":\"2024-10-27T21:21:01.9653733+00:00\"},\"t\":638656572619653733}" 5) "sldexp" 6) "-1"

Any idea what could be going wrong? Using newtonsoft serializer, redis backplane/L2

jimmycartrette commented 3 hours ago

I think the default JsonSerializerSettings needs DefaultValueHandling to be set:

                var serializer = new FusionCacheNewtonsoftJsonSerializer(new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.Auto,
                    DefaultValueHandling = DefaultValueHandling.Include
                });
jodydonetti commented 3 hours ago

Hi @jimmycartrette , yes I was about to suggest looking into the configuration of the specific serializer. Glad it now works.

Just to be clear, regarding the code above which one is:

I want to doublecheck something...

jimmycartrette commented 2 hours ago

The first one, not a nullable value type

jodydonetti commented 2 hours ago

What I thought: I don't if this is exactly the case, but in general if you try do deserialize null into a bool you will have an exception.