-
Notifications
You must be signed in to change notification settings - Fork 2
[Concert] Redis 기반 공연 제목 자동완성 #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ebee07
feat: 자동완성 기능 완료, 정리 필요
Creamcheesepie 684cc8a
feat: 공연 검색어 자동 완성 컨트롤러 수정
Creamcheesepie ef16fe1
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-…
Creamcheesepie 402cbaf
Merge branch 'feat/#177' into feat/#168
Creamcheesepie ce8639c
feat: 검색어 자동완성 로직 수정 -> pipeline 이용 1번의 IO로 처리함,
Creamcheesepie 4c53b0b
feat: 군더더기 코드 삭제 및 메소드 명 변경
Creamcheesepie 80130db
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-…
Creamcheesepie 1158af7
feat : @EnableScheduling 어노테이션 Applictaion 시작지점에 삽입.
Creamcheesepie 9f38583
fix : 오타 수정
Creamcheesepie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/back/web7_9_codecrete_be/domain/concerts/dto/concert/AutoCompleteItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.back.web7_9_codecrete_be.domain.concerts.dto.concert; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class AutoCompleteItem { | ||
| private String name; | ||
| private Long Id; | ||
|
|
||
| public AutoCompleteItem(String name, Long id) { | ||
| this.name = name; | ||
| Id = id; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/back/web7_9_codecrete_be/domain/concerts/dto/concert/WeightedString.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.back.web7_9_codecrete_be.domain.concerts.dto.concert; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.concerts.entity.Concert; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class WeightedString { | ||
| private Long concertId; | ||
| private String word; | ||
| private double score; | ||
|
|
||
| public WeightedString(String word, int score) { | ||
| this.word = word; | ||
| this.score = score; | ||
| } | ||
|
|
||
| public WeightedString(Concert concert) { | ||
| this.concertId = concert.getConcertId(); | ||
| this.word = concert.getName(); | ||
| this.score = ((double)concert.getViewCount()) * 0.1; | ||
| } | ||
|
|
||
|
|
||
| } |
74 changes: 74 additions & 0 deletions
74
...a/com/back/web7_9_codecrete_be/domain/concerts/repository/ConcertSearchRedisTemplate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.back.web7_9_codecrete_be.domain.concerts.repository; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.concerts.dto.concert.AutoCompleteItem; | ||
| import com.back.web7_9_codecrete_be.domain.concerts.dto.concert.WeightedString; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.redis.core.RedisCallback; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @Slf4j | ||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class ConcertSearchRedisTemplate { | ||
| private final RedisTemplate<String,String> redisTemplate; | ||
|
|
||
| private static final String INDEX_KEY = "index:"; | ||
| private static final String DATE_KEY = "data:"; | ||
| private static final String CONCERT_ID_KEY = "concertName:"; | ||
|
|
||
| public void addAllWordsWithWeight(List<WeightedString> weightedStrings) { | ||
| // PipeLine 사용해서 한번에 처리 -> IO 시간 감소 | ||
| redisTemplate.executePipelined((RedisCallback<?>) connection ->{ | ||
| for (WeightedString weightedString : weightedStrings) { | ||
| String id = String.valueOf(weightedString.getConcertId()); | ||
| String word = weightedString.getWord(); | ||
| double score = weightedString.getScore(); | ||
|
|
||
| // 개별 문자들에 대한 키-값 설정 | ||
| connection.commands().set((CONCERT_ID_KEY + word).getBytes(StandardCharsets.UTF_8), id.getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| for(int i = 0 ;i<word.length();i++){ | ||
| for(int j = i+1;j<= word.length();j++ ){ | ||
| String subWord = word.substring(i,j); | ||
|
|
||
| // 공백은 검색어 인덱스에서 제외 | ||
| if(subWord.isBlank()) continue; | ||
|
|
||
| byte[] indexKey = (INDEX_KEY + subWord).getBytes(StandardCharsets.UTF_8); | ||
| connection.zAdd(indexKey,score,word.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| public List<AutoCompleteItem> getAutoCompleteWord(String keyword, int start, int end) { | ||
| Set<String> results = redisTemplate.opsForZSet().reverseRange(INDEX_KEY + keyword, 0, 9); | ||
| List<String> resultList = new ArrayList<>(results); | ||
| return resultList.stream().map(name ->{ | ||
| Long id = Long.valueOf(redisTemplate.opsForValue().get(CONCERT_ID_KEY + name)); | ||
| return new AutoCompleteItem(name,id); | ||
| }).toList(); | ||
| } | ||
|
|
||
| public void deleteAutoCompleteWords() { | ||
| Set<String> keys = redisTemplate.keys("index:*"); | ||
| Set<String> datas = redisTemplate.keys("data:*"); | ||
| if (keys != null || !keys.isEmpty()) redisTemplate.delete(keys); | ||
| if (datas != null || !keys.isEmpty()) redisTemplate.delete(datas); | ||
| } | ||
|
|
||
| public Long getConcertIdByName(String concertName) { | ||
| String raw = redisTemplate.opsForValue().get(CONCERT_ID_KEY + concertName); | ||
| return raw == null ? null : Long.parseLong(raw); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/back/web7_9_codecrete_be/global/scheduler/ConcertScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.back.web7_9_codecrete_be.global.scheduler; | ||
|
|
||
| import com.back.web7_9_codecrete_be.domain.concerts.controller.ConcertController; | ||
| import com.back.web7_9_codecrete_be.domain.concerts.repository.ConcertSearchRedisTemplate; | ||
| import com.back.web7_9_codecrete_be.domain.concerts.service.ConcertNotifyService; | ||
| import com.back.web7_9_codecrete_be.domain.concerts.service.ConcertService; | ||
| import com.back.web7_9_codecrete_be.domain.concerts.service.KopisApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class ConcertScheduler { | ||
| private final ConcertService concertService; | ||
| private final KopisApiService kopisApiService; | ||
| private final ConcertNotifyService concertNotifyService; | ||
|
|
||
| // 공연 데이터 업데이트를 진행합니다. | ||
| @Scheduled(cron = "0 0 2 * * *") | ||
| public void concertUpdateSchedule() throws InterruptedException { | ||
| kopisApiService.updateConcertData(); | ||
| } | ||
|
|
||
| // 공연 관련 정보를 갱신합니다. | ||
| @Scheduled(cron = "0 0 3 * * *") | ||
| public void concertDataUpdateSchedule() { | ||
| concertService.viewCountUpdate(); | ||
| concertService.resetAutoComplete(); | ||
| concertService.setAutoComplete(); | ||
| } | ||
|
|
||
| // 이메일 알림을 전송합니다. | ||
| @Scheduled(cron = "0 0 9 * * *") | ||
| public void notificationSendSchedule() { | ||
| concertNotifyService.sendTodayTicketingConcertsNotifyingEmail(); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.