Spring caching - don't cache on error

I am using spring caching, my question is:

How can I control caching in case the result is an error and the next request might be good?

Example:

@Cacheable("mycache")
public  ResponceBO getBigObject(String id) throws Exception {

    boolean isError = false;

    ***   load big object from other service,  can be loaded with errors  ***
    isError = true;

    if(error){
       responceBO.setError(true);
    }

    return responceBO;
}

      

In case of error, I don't want to cache the object, what can I do instead?

+3


source to share


1 answer


I would suggest reading the Conditional Caching section of the reference manual. Basically just provide an expression unless

to prevent caching. (The placeholder result

is only available to the operator unless

, not the operator condition

.

@Cacheable("mycache", unless="#result.error")
public  ResponceBO getBigObject(String id) throws Exception { ... }

      



Must do the trick.

+3


source







All Articles