How can I ensure that 2nd level cache is enabled in spring boot app?

59 Views Asked by At

How can I ensure that 2nd level cache is enabled in my Spring Boot app or how can I know that it already worked? I'm using Ehcache and Spring data JPA pattern for the Domain layer.

by using these method,

    public Book fetchById(Long id){
        Optional<Book> book = bookRepository.fetchById(id);
        if (book.isEmpty()){

            System.out.println("book not found");
        }

        return book.orElse(null);

    }

    public Book findById(Long id){

        Optional<Book> book = bookRepository.findById(id);
        if (book.isEmpty()){

            System.out.println("book not found");
        }

        return book.get();
    }

In application.properties class config:

hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_query_cache=true
spring.cache.ehcache.config=classpath:ehcache.xml

Book repository:

@Repository
@Transactional
public interface BookRepository extends JpaRepository<Book, Long> {
    @Override
    @Transactional
    @EntityGraph(value = "book-author", type = EntityGraph.EntityGraphType.LOAD)
    List<Book> findAll();

    @org.springframework.transaction.annotation.Transactional(propagation = Propagation.REQUIRES_NEW)
    @Cacheable(value = "book")
    @Query(value = "SELECT b FROM Book b JOIN FETCH b.author a WHERE b.id = :id")
    Optional<Book> fetchById(@Param("id") Long id);

}

Book Entity:

@Table(name = "book")
@NamedEntityGraph(name = "book-author" , attributeNodes = @NamedAttributeNode("author"))
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true, nullable = false)
    private String isbn;

    @Column(nullable = false)
    private String title;
    private String category;

    private Double price;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author_id")
    private Author author;

{ getter & setter, etc... }

}

BookService.class:


@Service
@Transactional
public class BookService {

    private static final Logger logger = LoggerFactory.getLogger(BookService.class);

    BookRepository bookRepository;
    @Autowired
    public BookService(BookRepository authorRepository) {
        this.bookRepository = authorRepository;
    }

    public Book fetchById(Long id){
        Optional<Book> book = bookRepository.fetchById(id);
        if (book.isEmpty()){

            System.out.println("book not found");
        }

        return book.orElse(null);

    }

    public Book findById(Long id){

        Optional<Book> book = bookRepository.findById(id);
        if (book.isEmpty()){

            System.out.println("book not found");
        }

        return book.get();
    }

Pom.xml:

        <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>3.10.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-ehcache -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>6.0.0.Alpha7</version>
        </dependency>

0

There are 0 best solutions below