spring - How to make @Cacheable return null when not found in cache but do not cache the result? -


following @cacheable annotation

@cacheable(value="books", key="#isbn") public book findbook(isbn isbn, boolean checkwarehouse, boolean includeused) 
  • looks in cache based on key
  • if found return result cache
  • if not found evaluates method
  • updates cache
  • return result

so next time method called same arguments fetched cache.

what want instead to

  • look in cache based on key
  • if found return result cache
  • if not found return null

i don't want update cache in case of cache-miss, there way using spring annotation

here ended with, trick use 'unless' our advantage

@cacheable(value="books", key="#isbn", unless = "#result == null") public book findbookfromcache(isbn isbn, boolean checkwarehouse, boolean includeused) {     return null; } 

this method

  • looks in cache based on key
  • if found return result cache
  • if not found evaluates method return null
  • return value matched unless condition (which return true)
  • null value not cached
  • null value returned caller

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -