Skip to content

Commit

Permalink
imp: api latency 최적화
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 23, 2023
1 parent bb64ff7 commit 059790a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/main/java/com/flickspick/home/application/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import com.flickspick.ott.service.OttService;
import com.flickspick.recommendtype.application.RecommendTypeService;
import com.flickspick.recommendtype.model.RecTypeModel;
import com.flickspick.user.application.UserService;
import com.flickspick.user.infrastructure.UserRepository;
import com.flickspick.user.model.UserModel;
import com.flickspick.user_movie_history.application.UserMovieHistoryService;
import com.flickspick.user_movie_history.domain.UserMovieHistory;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -30,6 +30,7 @@ public class HomeService {
private final RecommendTypeService recommendTypeService;
private final MovieService movieService;
private final OttService ottService;
private final UserService userService;

public MyHomeResponse getHome(AuthUser authUser) {
var user = userRepository.findById(authUser.getId())
Expand All @@ -50,25 +51,21 @@ public MyHomeResponse getHome(AuthUser authUser) {
}

public MyProfileResponse getMyProfile(AuthUser authUser) {
var user = userRepository.findById(authUser.getId())
.orElseThrow(() -> new UserNotFoundException(ErrorType.USER_NOT_FOUND_ERROR));
UserModel userModel = UserModel.from(user);
UserMovieHistory userMovieHistory = userMovieHistoryService.getRecentHistory(user.getId());
RecTypeModel recTypeModel = recommendTypeService.getRecTypeModel(userMovieHistory.getRecommendTypeId());
var userModel = userService.getUserModel(authUser.getId());

UserMovieHistory userMovieHistory = userMovieHistoryService.getRecentHistory(userModel.getId());
RecTypeModel recTypeModel = recommendTypeService.get(userMovieHistory.getRecommendTypeId());

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

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

List<OttModel> ottModelList = ottService.findAllByUid(user.getId())
.stream().map(
ottUser -> ottService.findById(ottUser.getOttId())
).collect(Collectors.toList())
.stream().map(
ott -> OttModel.toModel(ott)
).collect(Collectors.toList());
List<OttModel> ottModelList = ottService.findAllByUid(userModel.getId())
.stream()
.map(ottUser -> ottService.get(ottUser.getOttId()))
.collect(Collectors.toList());
return MyProfileResponse.toResponse(userModel, ottModelList, List.of(recTypeModel), tags, similarMovies);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/flickspick/ott/service/OttService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,49 @@
import com.flickspick.ott.dto.OttsResponse;
import com.flickspick.ott.infrastructure.OttRepository;
import com.flickspick.ott.infrastructure.OttUserRepository;
import com.flickspick.ott.model.OttModel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class OttService {
private final OttRepository ottRepository;
private final OttUserRepository ottUserRepository;
private Map<Long, OttModel> otts;

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

public Map<Long, OttModel> refresh() {
return ottRepository.findAll()
.stream()
.map(OttModel::toModel)
.collect(Collectors.toMap(OttModel::getId, Function.identity()));
}

public OttModel get(Long id) {
var ott = otts.get(id);

if (ott == null) {
throw new OttNotFoundException(ErrorType.OTT_NOT_FOUND_ERROR);
}

return ott;
}

public OttsResponse getAll() {
var otts =
Expand Down

0 comments on commit 059790a

Please sign in to comment.