Today, when annotated with cacheable, null values are found and cached. The next time another system updates the value, from the cache, or the null value, there is a problem. So on the one hand, you want the other system to update, to clear the cache, on the other hand do not want to cache too much junk data.
Reference http://stackoverflow.com/questions/12113725/ How-do-i-tell-spring-cache-not-to-cache-null-value-in-cacheable-annotation the procedure and make changes to the program
@Cacheable(key = "''+#id", unless="#result == null")
public XXXPO get(int id) {
//get from db
}
This way, when a method returns a null value, it is not cached. See unless explanation:
/**
* Spring Expression Language (SpEL) attribute used to veto method caching.
* <p>Unlike {@link #condition()}, this expression is evaluated after the method
* has been called and can therefore refer to the {@code result}. Default is "",
* meaning that caching is never vetoed.
* @since 3.2
*/
The effect is to use the value of result to decide whether to negate the method cache. So it can be used to make conditional judgments.