-
Notifications
You must be signed in to change notification settings - Fork 54
[#61] 최근 방문한 매장 조회 기능 구현 #62
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.delfood.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.GenericToStringSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class ShopRedisConfig { | ||
|
||
@Bean | ||
public LettuceConnectionFactory shopRedisConnectionFactory() { return new LettuceConnectionFactory(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LettuceConnectionFactory는 따로 옵션을 설정해 주자 않는건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. application.propertise에 spring.redis.port=6379 와 같이 설정을 해놓으면 자동으로 적용되어 따로 옵션을 설정해주진 않았습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bean이 제대로 주입되지 않아서 자동으로 적용되는것처럼 보이는 것 같습니다. 설정도 따로 분리하시는게 좋을 것 같네요. 명시적으로 config를 설정해주지않으면 자동으로 적용되지 않습니다 |
||
|
||
@Bean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오류가 나는 이유는 동일 타입 Bean에 이름을 정해주지 않아서입니다. 해당 Bean에 이름을 정해준다면 오류를 해결할 수 있을것으로 보이네요. 다만 그렇다면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇군요. 참고해서 다시 적용해보겠습니다 |
||
public RedisTemplate<String, Long> shopRedisTemplate() { | ||
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(shopRedisConnectionFactory()); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class)); | ||
return redisTemplate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 각 옵션들을 설정한 의도를 주석으로 설명해주시면 좋을 것 같습니다~ |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.delfood.dao; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ZSetOperations; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Repository | ||
public class RecentShopViewDao { | ||
|
||
private static final String RECENT_SHOP_VIEW_KEY = "recent:view:shop:"; | ||
|
||
// 최대 조회 가능한 매장수 | ||
private static final int MAX_LIST_SIZE = 10; | ||
|
||
@Autowired | ||
private RedisTemplate<String, Long> shopRedisTemplate; | ||
|
||
/** | ||
* 방문한 매장 정보를 추가한다. | ||
* | ||
* @param memberId 회원 아이디 | ||
* @param shopId 매장 아이디 | ||
*/ | ||
public void add(String memberId, Long shopId) { | ||
ZSetOperations<String, Long> zSetOperations = shopRedisTemplate.opsForZSet(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ZSet을 사용한 이유가 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 최근 조회한 매장 목록을 불러와야 되는데 list를 사용하면 데이터의 중복이 발생하고 중복을 막기위해 그냥 set을 사용하면 최근 조회한 매장 순서로 불러올 수 가 없었기 때문에 정렬된 셋인 ZSet을 사용하였습니다. |
||
zSetOperations.add(RECENT_SHOP_VIEW_KEY + memberId, shopId, System.currentTimeMillis()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 키를 만드는 Factory가 있으면 좋을 것 같습니다. 여러 곳에서 같은 key를 참조해야할텐데 동일한 key 생성 로직을 하나의 메서드로 만들 수 있으니까요. |
||
zSetOperations.removeRange(RECENT_SHOP_VIEW_KEY + memberId, MAX_LIST_SIZE + 1, -1); | ||
} | ||
|
||
/** | ||
* 최근 조회한 매장목록을 조회한다. | ||
* 키에 해당하는 매장목록이 없는 경우 빈 리스트 반환 | ||
* | ||
* @param memberId 회원 아이디 | ||
* @return 최근 조회한 매장 목록 | ||
*/ | ||
public List<Long> getRecentShopView(String memberId) { | ||
return new ArrayList<>(Objects.requireNonNull( | ||
shopRedisTemplate.opsForZSet().reverseRange(RECENT_SHOP_VIEW_KEY + memberId, 0, -1))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.delfood.service; | ||
|
||
import com.delfood.dao.RecentShopViewDao; | ||
import com.delfood.dto.ShopDTO; | ||
import com.delfood.mapper.ShopMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class RecentShopViewService { | ||
|
||
@Autowired | ||
RecentShopViewDao recentShopViewDao; | ||
|
||
@Autowired | ||
ShopMapper shopMapper; | ||
|
||
public void add(String memberId, Long shopId) { | ||
recentShopViewDao.add(memberId, shopId); | ||
} | ||
|
||
public List<ShopDTO> getRecentShopView(String memberId) { | ||
List<Long> shopIdList = recentShopViewDao.getRecentShopView(memberId); | ||
return shopMapper.findByIdList(shopIdList); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 레디스 설정파일이 2개일 때 오류가 나서 임시로 지웠는데 해결하면 다시 추가하겠습니다.