-
Notifications
You must be signed in to change notification settings - Fork 37
[3주차] 예약 API 구현 및 Redisson Lock 구현 #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jmParkGit
Are you sure you want to change the base?
Changes from 2 commits
5a8f4f8
c9bb162
86d2e91
56b5943
2759e17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.movie.app.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class TicketingRequestDto { | ||
| private List<Integer> seats; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,12 +1,18 @@ | ||||||||||||||||||||||
| package com.movie.app.service; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import org.redisson.api.RLock; | ||||||||||||||||||||||
| import org.redisson.api.RedissonClient; | ||||||||||||||||||||||
| import org.springframework.cache.annotation.Cacheable; | ||||||||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import com.movie.app.domain.Movie; | ||||||||||||||||||||||
| import com.movie.app.domain.MovieRepository; | ||||||||||||||||||||||
| import com.movie.app.domain.MovieRequestDto; | ||||||||||||||||||||||
| import com.movie.app.domain.TicketingRequestDto; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -15,6 +21,7 @@ | |||||||||||||||||||||
| public class MovieService { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private final MovieRepository movieRepository; | ||||||||||||||||||||||
| private final RedissonClient redissonClient; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Cacheable(value = "Movies", key = "#title", cacheManager = "contentCacheManager") | ||||||||||||||||||||||
| public List<Movie> getMoviesByTitle(String title) { | ||||||||||||||||||||||
|
|
@@ -27,8 +34,40 @@ public List<Movie> getMoviesByGenre(String genre) { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Cacheable(value = "Movies", key = "all", cacheManager = "contentCacheManager") | ||||||||||||||||||||||
| public List<Movie> getMoviesByGenre() { | ||||||||||||||||||||||
| public List<Movie> getMoviesAll() { | ||||||||||||||||||||||
| return movieRepository.findAll(); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public Movie postMovie(MovieRequestDto requestDto) { | ||||||||||||||||||||||
| Movie movie = new Movie(requestDto); | ||||||||||||||||||||||
| movieRepository.save(movie); | ||||||||||||||||||||||
| return movie; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||
| public Movie ticketing(Long id, TicketingRequestDto requestDto) { | ||||||||||||||||||||||
| Movie movie = movieRepository.findById(id).orElseThrow( | ||||||||||||||||||||||
| () -> new NullPointerException("There is no id at DB.") | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if(movie==null) { | ||||||||||||||||||||||
| return movie; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+49
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
movie에 대해 orElseThrow를 하셨다면 movie가 null일 수는 없겠네요! |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| RLock lock = redissonClient.getLock(id.toString()); | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 영화 전체에 대한 잠금이 걸려있네요! |
||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| boolean acquireLock = lock.tryLock(10, 1, TimeUnit.SECONDS); | ||||||||||||||||||||||
| if (!acquireLock) { | ||||||||||||||||||||||
| System.out.println("Lock get fail"); | ||||||||||||||||||||||
| return movie; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+60
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예약이 실패됐다 라는 내용이 사용자에게 전달되려면 어떻게 해야 할까요? |
||||||||||||||||||||||
| movie.updateSeats(requestDto.getSeats()); | ||||||||||||||||||||||
| } catch (InterruptedException e) { | ||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||
| lock.unlock(); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return movie; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 이 부분 테스트 해보셨을까요?
boolean 배열이 디비에 저장되기 위해선 적절한 변환 과정이 선언되어야 할듯 합니다.