From af9424f758b437f5e0707dde69d8ac4ad267d36c Mon Sep 17 00:00:00 2001 From: shreya kore Date: Thu, 10 Oct 2024 22:04:14 +0530 Subject: [PATCH 1/2] feat: Add caching to BookService and MemberService - Implemented @Cacheable and @CacheEvict annotations in BookService and MemberService. - Added caching dependency to pom.xml. - Enabled caching with @EnableCaching annotation. --- pom.xml | 9 +++++++-- .../LibrarymanApiApplication.java | 2 ++ .../com/libraryman_api/book/BookService.java | 19 +++++++++++++++++++ .../libraryman_api/member/MemberService.java | 8 ++++++++ .../application-development.properties | 1 - .../application-production.properties | 2 +- 6 files changed, 37 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index b916177..eb88785 100644 --- a/pom.xml +++ b/pom.xml @@ -27,11 +27,10 @@ - 17 + 20.0.1 - org.springframework.boot spring-boot-starter-data-jpa @@ -69,6 +68,12 @@ org.springframework.boot spring-boot-starter-mail + + + org.springframework.boot + spring-boot-starter-cache + + diff --git a/src/main/java/com/libraryman_api/LibrarymanApiApplication.java b/src/main/java/com/libraryman_api/LibrarymanApiApplication.java index 9daf51e..042e728 100644 --- a/src/main/java/com/libraryman_api/LibrarymanApiApplication.java +++ b/src/main/java/com/libraryman_api/LibrarymanApiApplication.java @@ -2,12 +2,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableAsync @EnableScheduling +@EnableCaching public class LibrarymanApiApplication { public static void main(String[] args) { diff --git a/src/main/java/com/libraryman_api/book/BookService.java b/src/main/java/com/libraryman_api/book/BookService.java index 3cb288b..fc919d1 100644 --- a/src/main/java/com/libraryman_api/book/BookService.java +++ b/src/main/java/com/libraryman_api/book/BookService.java @@ -2,6 +2,9 @@ import java.util.Optional; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mapping.PropertyReferenceException; @@ -46,6 +49,8 @@ public BookService(BookRepository bookRepository) { * @return a {@link Page} of {@link Book} representing all books * @throws InvalidSortFieldException if an invalid sortBy field is specified */ + + @Cacheable(value = "books") public Page getAllBooks(Pageable pageable) { try { Page pagedBooks = bookRepository.findAll(pageable); @@ -61,6 +66,8 @@ public Page getAllBooks(Pageable pageable) { * @param bookId the ID of the book to retrieve * @return an {@code Optional} containing the found book, or {@code Optional.empty()} if no book was found */ + + @Cacheable(value = "books", key ="#bookId") public Optional getBookById(int bookId) { Optional bookById = bookRepository.findById(bookId); @@ -73,6 +80,8 @@ public Optional getBookById(int bookId) { * @param bookDto the book to be added * @return the saved book */ + + @CacheEvict(value = "books", allEntries = true) public BookDto addBook(BookDto bookDto) { Book book = DtoToEntity(bookDto); Book savedBook = bookRepository.save(book); @@ -87,6 +96,11 @@ public BookDto addBook(BookDto bookDto) { * @return the updated book * @throws ResourceNotFoundException if the book with the specified ID is not found */ + + @Caching(evict = { + @CacheEvict(value = "books", key = "#bookId"), // Evict the specific book cache + @CacheEvict(value = "books", allEntries = true) // Evict the list cache + }) public BookDto updateBook(int bookId, BookDto bookDtoDetails) { Book book = bookRepository.findById(bookId) .orElseThrow(() -> new ResourceNotFoundException("Book not found")); @@ -107,6 +121,11 @@ public BookDto updateBook(int bookId, BookDto bookDtoDetails) { * @param bookId the ID of the book to delete * @throws ResourceNotFoundException if the book with the specified ID is not found */ + + @Caching(evict = { + @CacheEvict(value = "books", key = "#bookId"), // Evict the specific book cache + @CacheEvict(value = "books", allEntries = true) // Evict the list cache + }) public void deleteBook(int bookId) { Book book = bookRepository.findById(bookId) .orElseThrow(() -> new ResourceNotFoundException("Book not found")); diff --git a/src/main/java/com/libraryman_api/member/MemberService.java b/src/main/java/com/libraryman_api/member/MemberService.java index c6505d1..50262b8 100644 --- a/src/main/java/com/libraryman_api/member/MemberService.java +++ b/src/main/java/com/libraryman_api/member/MemberService.java @@ -2,6 +2,8 @@ import java.util.Optional; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mapping.PropertyReferenceException; @@ -67,6 +69,8 @@ public Page getAllMembers(Pageable pageable) { * @param memberId the ID of the member to retrieve * @return an {@code Optional} containing the found member, or {@code Optional.empty()} if no member was found */ + + @Cacheable(value = "members", key = "#memberId") public Optional getMemberById(int memberId) { Optional memberById = memberRepository.findById(memberId); @@ -103,6 +107,8 @@ public MembersDto addMember(MembersDto membersDto) { * @return the updated member record * @throws ResourceNotFoundException if the member is not found */ + + @CacheEvict(value = "members", key = "#memberId") public MembersDto updateMember(int memberId, MembersDto membersDtoDetails) { Members member = memberRepository.findById(memberId) .orElseThrow(() -> new ResourceNotFoundException("Member not found")); @@ -127,6 +133,8 @@ public MembersDto updateMember(int memberId, MembersDto membersDtoDetails) { * @param memberId the ID of the member to delete * @throws ResourceNotFoundException if the member is not found */ + + @CacheEvict(value = "members", key = "#memberId") public void deleteMember(int memberId) { Members member = memberRepository.findById(memberId) .orElseThrow(() -> new ResourceNotFoundException("Member not found")); diff --git a/src/main/resources/application-development.properties b/src/main/resources/application-development.properties index cd5618e..aedaf67 100644 --- a/src/main/resources/application-development.properties +++ b/src/main/resources/application-development.properties @@ -46,4 +46,3 @@ spring.mail.properties.domain_name=Add_Your_Mail_Service_Domain_Name - diff --git a/src/main/resources/application-production.properties b/src/main/resources/application-production.properties index 4d50b9f..6b8edc4 100644 --- a/src/main/resources/application-production.properties +++ b/src/main/resources/application-production.properties @@ -14,4 +14,4 @@ spring.mail.username=${MAIL_SERVICE_USERNAME} spring.mail.password=${MAIL_SERVICE_PASSWORD} spring.mail.properties.mail.smtp.auth=${MAIL_SERVICE_SMTP} spring.mail.properties.mail.starttls.enable=${MAIL_SERVICE_STARTTLS} -spring.mail.properties.domain_name=${MAIL_SERVICE_DOMAIN_NAME} +spring.mail.properties.domain_name=${MAIL_SERVICE_DOMAIN_NAME} \ No newline at end of file From 67bb3478456711ae5645343b09c6ba15c4536e50 Mon Sep 17 00:00:00 2001 From: shreya5653 <144722995+shreya5653@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:14:46 +0530 Subject: [PATCH 2/2] Update pom.xml --- pom.xml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index eb88785..5bc6c53 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ - 20.0.1 + 17 @@ -36,13 +36,11 @@ spring-boot-starter-data-jpa - org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-devtools @@ -50,7 +48,6 @@ true - com.mysql mysql-connector-j @@ -63,7 +60,6 @@ test - org.springframework.boot spring-boot-starter-mail