简体   繁体   中英

How do I tell Spring Cache to cache the exceptions in @Cacheable annotation?

I need 3 separate caches:

  • Response with some data
  • Null
  • Exception

I've already defined the two caches

@Caching(cacheable = {
    @Cacheable(value = "SomeCache", key = "#a1", unless = "#result == null"),
    @Cacheable(value = "SomeNullCache", key = "#a1", unless = "#result != null")})

So, I have to implement the last case.

JSR-107 provides @CacheResult annotation with exceptionCacheName attribute, but how can I do this using Spring Cache? I don't want to combine JSR-107 and Spring Cache.

The cache abstraction does not support caching exception throw by annotated method. Your setup looks very weird to me. Why would you use two different regions for null and non-null values?

Use the standard annotation if you want such setup.

I don't want to combine JSR-107 and Spring Cache.

The id generation being different (to be spec compliant and keeping backward compatibility), I wouldn't recommend such usage. At least not on the same region.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM