0
@CacheResult(cacheName = "testCache)
public Employee geteEmployee(@CacheKey String empId) {
   //logic
   return employee;
}

If employee is null, that null value should not be cached.

1 Answers1

0

You can set the cacheNull property to false.

cacheNull

public abstract boolean cacheNull

If set to false null return values will not be cached. If true (the default) null return values will be cached.

Defaults to true.

Default: true

@CacheResult(cacheName = "testCache", cacheNull = false)

peterulb
  • 2,869
  • 13
  • 20
  • cacheNull is not there in cacheResult interface. I am using below dependency version. javax.cache cache-api 1.1.1 – Geeks For geeks Aug 17 '22 at 09:32
  • "cacheNull" property is not there in latest "1.1.1" version. It is present in "1.0.0-PFD" version. – Geeks For geeks Aug 17 '22 at 09:42
  • If you use spring anyway, shouldn't you go with its own annotation? `@Cacheable(value="defaultCache", key="#pk", unless="#result == null")` – peterulb Aug 17 '22 at 09:43
  • We are using jsr-107 cache annotations in our application. @Cacheable annotation is present in org.springframework.cache. Any work around for CacheResult 1.1.1 version for not to cache null values? – Geeks For geeks Aug 17 '22 at 09:49
  • Not unless you are ready to use exceptions like `NoSuchElementException` instead of null. I'd hope future versions would include the attribute from PFD. – peterulb Aug 17 '22 at 09:56