Skip to content

Commit

Permalink
feat: jdbcTemplate을 이용하여 새로운 게시글 bulk insert하여 성능 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
inpink committed Nov 9, 2024
1 parent b507287 commit 360af9f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package knusearch.clear.jpa.repository.post;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;
import knusearch.clear.jpa.domain.post.BasePost;
import knusearch.clear.jpa.domain.post.Term;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class BasePostJdbcRepository {

private final JdbcTemplate jdbcTemplate;

public void saveAll(List<BasePost> basePosts) {
for (BasePost basePost : basePosts) {
KeyHolder keyHolder = new GeneratedKeyHolder();

jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(
"INSERT INTO base_post " +
"(url, site_name, scrt_wrti_yn, encrypted_menu_sequence, encrypted_menu_board_sequence, " +
"title, classification, text, image, image_text, date_time) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);

ps.setString(1, basePost.getUrl());
ps.setString(2, basePost.getSiteName());
ps.setBoolean(3, basePost.isScrtWrtiYn());
ps.setString(4, basePost.getEncryptedMenuSequence());
ps.setString(5, basePost.getEncryptedMenuBoardSequence());
ps.setString(6, basePost.getTitle());
ps.setString(7, basePost.getClassification());
ps.setString(8, basePost.getText());
ps.setString(9, basePost.getImage());
ps.setString(10, basePost.getImageText());
ps.setDate(11, java.sql.Date.valueOf(basePost.getDateTime()));

return ps;
}, keyHolder);

basePost.setId(keyHolder.getKey().longValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import knusearch.clear.jpa.domain.post.BasePost;
import knusearch.clear.jpa.domain.site.Board;
import knusearch.clear.jpa.domain.site.Site;
import knusearch.clear.jpa.repository.post.BasePostJdbcRepository;
import knusearch.clear.jpa.repository.post.BasePostRepository;
import knusearch.clear.jpa.service.post.CheckPostResult;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class ScrapingService {
}};
private final BasePostRepository basePostRepository;
private final ClassificationService classificationService;
private final BasePostJdbcRepository basePostJdbcRepository;

@Transactional
public String makeFinalPostListUrl(String baseUrl, String postUrl, int pageIdx) {
Expand Down Expand Up @@ -199,6 +201,7 @@ public String cutString(String text, int byteSize) {
}

public void scrapeYesterdayPosts(Site site) {
log.info("Scraping posts from " );
String baseUrl = site.getBaseUrl();
List<Board> boards = site.getBoards();

Expand All @@ -218,7 +221,7 @@ public void savePostsWithinPeriod(String baseUrl, String postUrl) {
CheckPostResult checkPostResult = checkWithinPeriod(baseUrl, postUrl, links);
isTimeToBreak = checkPostResult.isShouldBreak();
List<BasePost> linkedPosts = checkPostResult.getNewPosts();
// basePostJdbcRepository.saveAll(linkedPosts);
basePostJdbcRepository.saveAll(linkedPosts);
pageIdx++;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package knusearch.clear.jpa.service.post;

import java.util.List;
import knusearch.clear.jpa.domain.post.BasePost;
import knusearch.clear.jpa.domain.site.Site;
import knusearch.clear.jpa.service.ScrapingService;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,7 +14,7 @@ public class PostScrapingTask {

private final ScrapingService scrapingService;

@Scheduled(cron = "0 0 0 * * *")
@Scheduled(cron = "0 * * * * *")
public void performTask() {
scrapingService.scrapeYesterdayPosts(Site.MAIN);
}
Expand Down

0 comments on commit 360af9f

Please sign in to comment.