diff --git a/pom.xml b/pom.xml
index b916177..5bc6c53 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,19 +31,16 @@
-
org.springframework.boot
spring-boot-starter-data-jpa
-
org.springframework.boot
spring-boot-starter-web
-
org.springframework.boot
spring-boot-devtools
@@ -51,7 +48,6 @@
true
-
com.mysql
mysql-connector-j
@@ -64,11 +60,16 @@
test
-
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