Skip to content

Commit

Permalink
Merge pull request #19 from deepredk/feature/18
Browse files Browse the repository at this point in the history
처음 요청되는 URL인지 Redis로 확인 및 처음 요청이라면 MySQL 저장
  • Loading branch information
deepredk authored Oct 17, 2023
2 parents aa90f8c + 67e9008 commit db913c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.numble.hittracker.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class IsCreatedUrlRepository {
private final RedisTemplate<String, String> redisTemplate;

public Long create(String url) {
return redisTemplate
.opsForSet()
.add("created_url", url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.numble.hittracker.controller.dto.UrlHitInfoDto;
import it.numble.hittracker.entity.DailyHitLog;
import it.numble.hittracker.entity.Url;
import it.numble.hittracker.repository.IsCreatedUrlRepository;
import it.numble.hittracker.repository.DailyHitLogRepository;
import it.numble.hittracker.repository.HitRepository;
import it.numble.hittracker.repository.UrlRepository;
Expand All @@ -21,6 +22,7 @@ public class HitTrackerService {
private final UrlRepository urlRepository;
private final DailyHitLogRepository dailyHitLogRepository;
private final HitRepository hitRepository;
private final IsCreatedUrlRepository isCreatedUrlRepository;

public UrlHitInfoDto getHitInfo(String url) {
if (!isExistsUrl(url)) {
Expand All @@ -37,6 +39,10 @@ public UrlHitInfoDto getHitInfo(String url) {
@Async
public void hit(String url) {
hitRepository.hit(url);

if (!isExistsUrl(url)) {
track(url);
}
}

public List<DailyHitLog> getStatistics(String url) {
Expand Down Expand Up @@ -68,12 +74,13 @@ private void deleteOldDailyHitLogs(LocalDate today) {
.forEach(dailyHitLogRepository::delete);
}

@Async
private void track(String url) {
Url urlBeingTracked = new Url(url);
urlRepository.save(urlBeingTracked);
}

private boolean isExistsUrl(String url) {
return urlRepository.existsByUrl(url);
return isCreatedUrlRepository.create(url) == 1L;
}
}

0 comments on commit db913c3

Please sign in to comment.