-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from 2E1I/dev
최신 검색어 개발 완료, validation 오류 해결
- Loading branch information
Showing
11 changed files
with
182 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/e2i1/linkeepserver/config/RedisConfig.java
This file contains 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,36 @@ | ||
package com.e2i1.linkeepserver.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory(){ | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, String> redisTemplate() { | ||
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); | ||
return redisTemplate; | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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 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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/e2i1/linkeepserver/domain/users/dto/RecentSearchResDTO.java
This file contains 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,16 @@ | ||
package com.e2i1.linkeepserver.domain.users.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class RecentSearchResDTO { | ||
private List<String> recentSearchList; | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/e2i1/linkeepserver/domain/users/service/RecentSearchService.java
This file contains 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,66 @@ | ||
package com.e2i1.linkeepserver.domain.users.service; | ||
|
||
import com.e2i1.linkeepserver.common.exception.ApiException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.ListOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
import static com.e2i1.linkeepserver.common.error.ErrorCode.INDEX_OUT_OF_RANGE; | ||
import static com.e2i1.linkeepserver.common.error.ErrorCode.NULL_POINT; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RecentSearchService { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
private static final String RECENT_SEARCHES_KEY_PREFIX = "recent_searches:"; | ||
private static final int MAX_RECENT_SEARCHES = 10; | ||
private static final String DELETE_MARKER = "__DELETE__"; | ||
|
||
|
||
public void addSearchTerm(Long userId, String keyword) { | ||
String key = RECENT_SEARCHES_KEY_PREFIX + userId; | ||
|
||
List<String> searchList = redisTemplate.opsForList().range(key, 0, -1); | ||
|
||
if (searchList == null) { | ||
throw new ApiException(NULL_POINT, "해당 유저는 최근 검색 기록이 없습니다."); | ||
} | ||
|
||
// keyword가 이미 최근 검색어 목록에 있다면 중복 저장하지 않고 가장 앞으로 가져오기 위해 삭제 | ||
if (searchList.contains(keyword)) { | ||
redisTemplate.opsForList().remove(key, 0, keyword); | ||
} | ||
|
||
redisTemplate.opsForList().leftPush(key, keyword); | ||
|
||
redisTemplate.opsForList().trim(key, 0, MAX_RECENT_SEARCHES - 1); | ||
} | ||
|
||
public List<String> getRecentSearch(Long userId) { | ||
String key = RECENT_SEARCHES_KEY_PREFIX + userId; | ||
return redisTemplate.opsForList().range(key, 0, -1); | ||
|
||
} | ||
|
||
public void deleteRecentSearch(Long userId, int index) { | ||
String key = RECENT_SEARCHES_KEY_PREFIX + userId; | ||
ListOperations<String, String> listOps = redisTemplate.opsForList(); | ||
Long listSize = listOps.size(key); | ||
|
||
if (listSize == null) { | ||
throw new ApiException(NULL_POINT, "해당 유저는 최근 검색 기록이 없습니다."); | ||
} | ||
if (index >= listSize || index < 0) { | ||
throw new ApiException(INDEX_OUT_OF_RANGE); | ||
} | ||
|
||
listOps.set(key, index, DELETE_MARKER); | ||
listOps.remove(key, 0, DELETE_MARKER); //list 개수 10개 고정이기에 전체 탐색 오버헤드 작음 | ||
} | ||
|
||
|
||
} |
This file contains 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