Skip to content

Commit

Permalink
Merge pull request #72 from shreya5653/mybranch-shreya5653
Browse files Browse the repository at this point in the history
feat: Add caching to BookService and MemberService
  • Loading branch information
Guhapriya01 authored Oct 11, 2024
2 parents 3c0fd82 + 67bb347 commit c510f81
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,23 @@
</properties>
<dependencies>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>


<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
Expand All @@ -64,11 +60,16 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BookDto> getAllBooks(Pageable pageable) {
try {
Page<Book> pagedBooks = bookRepository.findAll(pageable);
Expand All @@ -61,6 +66,8 @@ public Page<BookDto> 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<BookDto> getBookById(int bookId) {

Optional<Book> bookById = bookRepository.findById(bookId);
Expand All @@ -73,6 +80,8 @@ public Optional<BookDto> 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);
Expand All @@ -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"));
Expand All @@ -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"));
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/libraryman_api/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,8 @@ public Page<MembersDto> 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<MembersDto> getMemberById(int memberId) {

Optional<Members> memberById = memberRepository.findById(memberId);
Expand Down Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application-development.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ spring.mail.properties.domain_name=Add_Your_Mail_Service_Domain_Name




2 changes: 1 addition & 1 deletion src/main/resources/application-production.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit c510f81

Please sign in to comment.