Skip to content

Commit

Permalink
Test: N+1 발생
Browse files Browse the repository at this point in the history
  • Loading branch information
hjinshin committed Nov 14, 2024
1 parent 7cdea4d commit da7dadd
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 372 deletions.
4 changes: 4 additions & 0 deletions src/main/java/supernova/whokie/profile/ProfileVisitCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public Long getHostId() {
return hostId;
}

public void add() {
dailyVisited++;
}

public int getDailyVisited() {
return dailyVisited;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ProfileVisitorRepository extends JpaRepository<ProfileVisitor,
boolean existsByHostIdAndVisitorIpAndModifiedAtBetween(
Long hostId, String visitorIp, LocalDateTime start, LocalDateTime end
);

boolean existsByHostIdAndVisitorIp(Long hostId, String visitorIp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public RedisCommand.Visited findVisitCountById(Long hostId) {
return RedisCommand.Visited.from(visitCount);
}


@Transactional(readOnly = true)
public ProfileVisitCount findVisitCountBasdayId(Long hostId) {
return profileVisitCountRepository.findByHostId(hostId)
.orElseThrow(
() -> new EntityNotFoundException(MessageConstants.USER_NOT_FOUND_MESSAGE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import supernova.whokie.profile.Profile;
import supernova.whokie.profile.ProfileVisitor;
import supernova.whokie.profile.constants.ProfileConstants;
import supernova.whokie.profile.infrastructure.repository.ProfileRepository;
import supernova.whokie.profile.infrastructure.repository.ProfileVisitCountRepository;
import supernova.whokie.profile.infrastructure.repository.ProfileVisitorRepository;
import supernova.whokie.redis.entity.RedisVisitCount;
import supernova.whokie.user.Users;

import java.time.LocalDateTime;

@RequiredArgsConstructor
@Service
public class ProfileWriterService {

private final ProfileRepository profileRepository;
private final ProfileVisitReadService profileVisitReadService;
private final ProfileVisitorRepository profileVisitorRepository;
private final ProfileVisitCountRepository profileVisitCountRepository;

@Transactional
public void saveFromKaKao(Users user) {
Expand All @@ -23,4 +32,25 @@ public void saveFromKaKao(Users user) {
profileRepository.save(profile);
}

@Transactional
public RedisVisitCount fdsfs(Long hostId, String visitorIp) {
var profileVisitCount = profileVisitReadService.findVisitCountBasdayId(hostId);

if (profileVisitorRepository.existsByHostIdAndVisitorIp(hostId, visitorIp)) {
profileVisitCount.add();
profileVisitCountRepository.save(profileVisitCount);
}
profileVisitorRepository.save(ProfileVisitor.builder()
.visitorIp(visitorIp)
.hostId(hostId)
.visitTime(LocalDateTime.now())
.build());

return RedisVisitCount.builder()
.hostId(hostId)
.dailyVisited(profileVisitCount.getDailyVisited())
.totalVisited(profileVisitCount.getTotalVisited())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import supernova.whokie.global.annotation.RedissonLock;
import supernova.whokie.profile.ProfileVisitCount;
import supernova.whokie.profile.infrastructure.repository.ProfileVisitCountRepository;
import supernova.whokie.profile.infrastructure.repository.ProfileVisitorRepository;
import supernova.whokie.profile.service.ProfileVisitReadService;
import supernova.whokie.profile.service.ProfileVisitWriterService;
import supernova.whokie.profile.service.ProfileWriterService;
import supernova.whokie.redis.entity.RedisVisitCount;
import supernova.whokie.redis.entity.RedisVisitor;
import supernova.whokie.redis.infrastructure.repository.RedisVisitCountRepository;
Expand All @@ -22,17 +27,25 @@ public class RedisVisitService {
private final RedisVisitorRepository redisVisitorRepository;
private final RedisVisitCountRepository redisVisitCountRepository;
private final ProfileVisitReadService profileVisitReadService;
private final ProfileVisitorRepository profileVisitorRepository;
private final ProfileVisitCountRepository profileVisitCountRepository;
private final ProfileWriterService profileWriterService;

@RedissonLock(value = "#hostId")
public RedisVisitCount visitProfile(Long hostId, String visitorIp) {
RedisVisitCount redisVisitCount = findVisitCountByHostId(hostId);
log.info("visitorIp: {}", visitorIp);
if(!checkVisited(hostId, visitorIp)) {
redisVisitCount.visit();
redisVisitCountRepository.save(redisVisitCount);
}
// 방문자 로그 기록
saveVisitor(hostId, visitorIp);
// RedisVisitCount redisVisitCount = findVisitCountByHostId(hostId);
// log.info("visitorIp: {}", visitorIp);
// if(!checkVisited(hostId, visitorIp)) {
// redisVisitCount.visit();
// redisVisitCountRepository.save(redisVisitCount);
// }
// // 방문자 로그 기록
// saveVisitor(hostId, visitorIp);

var redisVisitCount = profileWriterService.fdsfs(hostId, visitorIp);




return redisVisitCount;
}
Expand Down
Loading

0 comments on commit da7dadd

Please sign in to comment.