Hi Team,
How can we restrict 'Null' values to add into cache?
Because I have one use case that if the value is null then we have to grab the data, not from the cache.
Thanks in advance.
The cache does not allow storing null values. If you perform a computation and return null then this indicates the data doesn't exist and the caller receives null. So for example cache.get(key, function) or CacheLoader#load behave similar to ConcurrentHashMap#computeIfAbsent. An explicit put(key, null) is not allowed and will throw an exception.
Does that resolve your scenario?
Thanks, Ben,
Thanks for your quick reply.
You are right. Cache is not allowing the null values.
Actually, I was storing java.util.Optional so that's why I was not getting an exception.
Optional is a good technique for negative caching.
Can you clarify if you have an open question? I'm unsure how to proceed.
My question is resolved now.
Closing this question.
Most helpful comment
The cache does not allow storing null values. If you perform a computation and return null then this indicates the data doesn't exist and the caller receives null. So for example
cache.get(key, function)orCacheLoader#loadbehave similar toConcurrentHashMap#computeIfAbsent. An explicitput(key, null)is not allowed and will throw an exception.Does that resolve your scenario?