Skip to content

Commit

Permalink
imp: movie 데이터 local cache 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo committed Sep 23, 2023
1 parent 8efb78a commit f1f5c02
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/flickspick/home/application/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public MyHomeResponse getHome(AuthUser authUser) {

List<String> tags = recTypeModel.getTags();

List<MovieModel> similarMovies = movieService.getMovieModelList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.getMovieModel(userMovieHistory.getMovieId());
List<MovieModel> similarMovies = movieService.getList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.get(userMovieHistory.getMovieId());
similarMovies.add(movieModel);

List<MovieModel> differentMovies = movieService.getMovieModelList(userMovieHistory.getMovieId(), 3);
List<MovieModel> differentMovies = movieService.getList(userMovieHistory.getMovieId(), 3);

return MyHomeResponse.toResponse(userModel, List.of(recTypeModel), tags, similarMovies, differentMovies);
}
Expand All @@ -58,8 +58,8 @@ public MyProfileResponse getMyProfile(AuthUser authUser) {

List<String> tags = recTypeModel.getTags();

List<MovieModel> similarMovies = movieService.getMovieModelList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.getMovieModel(userMovieHistory.getMovieId());
List<MovieModel> similarMovies = movieService.getList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.get(userMovieHistory.getMovieId());
similarMovies.add(movieModel);

List<OttModel> ottModelList = ottService.findAllByUid(userModel.getId())
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/com/flickspick/movie/application/MovieService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,68 @@

import com.flickspick.exception.dto.ErrorType;
import com.flickspick.exception.movie.MovieNotFoundException;
import com.flickspick.exception.rec.RecommendTypeNotFoundException;
import com.flickspick.movie.domain.Movie;
import com.flickspick.movie.infrastructure.MovieRepository;
import com.flickspick.movie.model.MovieModel;
import com.flickspick.recommendtype.model.RecTypeModel;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class MovieService {
private final MovieRepository movieRepository;
private Map<Long, MovieModel> movieModels;

@Scheduled(fixedRate = 1000 * 60 * 5, initialDelayString = "0")
public void refreshMovieModels() {
log.info("refresh movieModels info start");
movieModels = refresh();
log.info("refresh movieModels info complete");
}

public MovieModel getMovieModel(Long movieId) {
var movie = movieRepository.findById(movieId)
.orElseThrow(() -> new MovieNotFoundException(ErrorType.MOVIE_NOT_FOUND_ERROR));
return MovieModel.toModel(movie);
public Map<Long, MovieModel> refresh() {
return movieRepository.findAll()
.stream()
.map(MovieModel::toModel)
.collect(Collectors.toMap(MovieModel::getId, Function.identity()));
}

@Async(value = "taskExecutor")
public CompletableFuture<MovieModel> asyncGetMovieModel(Long movieId) {
return CompletableFuture.completedFuture(getMovieModel(movieId));
public MovieModel get(Long id) {
var movieModel = movieModels.get(id);

if (movieModel == null) {
throw new MovieNotFoundException(ErrorType.MOVIE_NOT_FOUND_ERROR);
}

return movieModel;
}

public List<MovieModel> getMovieModelList(Long movieId, int count) {
List<Movie> movieList = movieRepository.findAll()
.stream().filter(movie -> movie.getId() != movieId)
public List<MovieModel> getList(Long movieId, int count) {
List<MovieModel> movieList = movieModels.keySet()
.stream().map(movieModels::get)
.filter(movieModel -> movieModel.getId() != movieId)
.collect(Collectors.toList());

Collections.shuffle(movieList);

int limit = Math.min(movieList.size(), count);

return movieList.subList(0, limit)
.stream()
.map(MovieModel::toModel)
.collect(Collectors.toList());
}

@Async(value = "taskExecutor")
public CompletableFuture<List<MovieModel>> asyncGetMovieModelList(Long movieId, int count) {
return CompletableFuture.completedFuture(getMovieModelList(movieId, count));
return movieList.subList(0, limit);
}

public Long getMovieCount() {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/flickspick/rec/application/RecService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ public RecResponse get(AuthUser user, RecRequest request) {

RecTypeModel recTypeModel = recommendTypeService.get(recommendTypeId);

var movieModelCf = movieService.asyncGetMovieModel(movieId);
var recMoviesCf = movieService.asyncGetMovieModelList(movieId, 3);

CompletableFuture.allOf(movieModelCf, recMoviesCf).join();
var movieModel = movieService.get(movieId);
var recMovies = movieService.getList(movieId, 3);

userMovieHistoryService.saveUserMovieHistory(userMovieHistory);

return RecResponse.toResponse(recTypeModel, movieModelCf.get(), recMoviesCf.get());
return RecResponse.toResponse(recTypeModel, movieModel, recMovies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public ShareResponse share(AuthUser authUser) {

RecTypeModel recTypeModel = recommendTypeService.getRecTypeModel(userMovieHistory.getRecommendTypeId());

List<MovieModel> similarMovies = movieService.getMovieModelList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.getMovieModel(userMovieHistory.getMovieId());
List<MovieModel> similarMovies = movieService.getList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.get(userMovieHistory.getMovieId());
similarMovies.add(movieModel);

return new ShareResponse(
Expand Down

0 comments on commit f1f5c02

Please sign in to comment.