0

Very simple and straight forward exception. is there any way to cache exception using caffeine/springboot ?

some specific exceptions in my method can be very time consuming... [404 for example] i wish i could cache it and avoid long processing

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • 1
    I think you would catch it, return null, and tell spring to cache null values. – Ben Manes Dec 03 '22 at 08:58
  • 1
    As there isn't any answer yet and also a bounty, you might want to add some specific problem example: https://stackoverflow.com/help/minimal-reproducible-example - caching exceptions is usually not an issue. Maybe this is an https://xyproblem.info/ ? – Clijsters Dec 16 '22 at 15:55
  • I want to add that caching exceptions might be very simple, but usually not effective, as they are unique to each other. – Clijsters Dec 16 '22 at 15:56

2 Answers2

0

A simple way to cache exceptions is to encapsulate the call, catch the exception and represent it as a value. I am just adding more details based on @ben-manes comment.

Approach 1: encapsulate the exception as a business object Approach 2: return null value or Optional object on exception. For caching null values, you need to explicitly enable caching of null values (refer here - Spring Boot Cacheable - Cache null values)

Here is an example based on Spring Caching (can be extended to Caffeine). The following class loads a Book entity object which may result in exception. The exception is handled and cached, so next time the same ISBN code (argument) is passed the return value (exception) is returned from the cache.

@Component
public class SimpleBookRepository implements BookRepository {

  @Override
  @Cacheable("books")
  public Book getByIsbn(String isbn) {
    Book book = loadBook(isbn);
    return book;
  }

  // Don't do this at home
  private void loadBook(String isbn) {
    Book book;
    try {
      //get book from DB here
      book = loadBook();//DB call. can throw exception.
    } catch (Exception e) {
      book = new Book("None found"); Approach 1 - //encapsulate error as an entity.
      book = null; // Approach 2 - set as null 
    }
    return book;
  }
}
Shankar
  • 2,625
  • 3
  • 25
  • 49
0
 1.  Very first time add these dependency in pom.xml
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
        <version>2.7.0</version>

    </dependency>
2. add @CacheConfig(cacheNames = {"customer"}) , @Slf4j under @Service 
   annotation
3. add @Cacheable on your method where you want caching. and in method add log
   public Customer saveCustomer (Customer customerId) {
        log.info("Inside save Customer method of Customer Service");
        return customerRepository.save(customerId);
    }

There are a few important point:

The @CacheConfig is a class level annotation and help to streamline caching configurations. The @Cacheable annotation used to demarcate methods that are cacheable. In simple words, this annotation used to show caching API that we want to store results for this method into the cache so, on subsequent invocations, the value in the cache returned without execute the method.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '22 at 15:54