I need to cache null values with Ehcache 3. For Ehcache 2 I found examples like here:
// cache an explicit null value:
cache.put(new Element("key", null));
Element element = cache.get("key");
if (element == null) {
// nothing in the cache for "key" (or expired) ...
} else {
// there is a valid element in the cache, however getObjectValue() may be null:
Object value = element.getObjectValue();
if (value == null) {
// a null value is in the cache ...
} else {
// a non-null value is in the cache ...
Are there examples for this for Ehcache 3 as it seems net.sf.ehcache.Element doesn't exist anymore?
I have also seen this comment: https://github.com/ehcache/ehcache3/issues/1607
Indeed, you cannot cache a null value, this is also the behaviour of the JCache specification. If you need this in your application, either create a sentinel value or wrap your values from your application.
Of course I could build some logic if my return object is null put it to an other Set were I store only my keys for null elements. Of course also for reading I need to check my ehcache and my "special" Set.
Your question contains the answer, you need either to use the null object pattern or a related solution to wrap / hide your null
s.
There is no, and there will not be, support for null
key or values in Ehcache 3.