-
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 #4 from Footprints-Plan/aurora-db
[FP-58] Feat: 랭킹 기능 구현
- Loading branch information
Showing
8 changed files
with
155 additions
and
10 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
Submodule config
updated
from 0605e4 to 5d2acf
10 changes: 10 additions & 0 deletions
10
src/main/java/com/footprints/api/config/redis/BaseRedisRepository.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,10 @@ | ||
package com.footprints.api.config.redis; | ||
|
||
public abstract class BaseRedisRepository { | ||
protected Class<?> classInstance; | ||
|
||
abstract protected void init(); | ||
protected String generateGlobalKey(String localKey) { | ||
return classInstance.getSimpleName().substring(0,classInstance.getSimpleName().length()-14) + "-" + localKey; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/footprints/api/config/redis/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,60 @@ | ||
package com.footprints.api.config.redis; | ||
|
||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
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.listener.ChannelTopic; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.database}") | ||
private int database; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(host, port); | ||
connectionFactory.setDatabase(database); | ||
return connectionFactory; | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
ObjectMapper objectMapper = new ObjectMapper() | ||
.findAndRegisterModules() | ||
.enable(SerializationFeature.INDENT_OUTPUT) // 들여쓰기를 활성화 | ||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) // 날짜를 timestamp 변환하지 않도록 함 | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, | ||
true) // 데이터가 불일치하는 상황이면 예외를 발생시킴 | ||
.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, | ||
"@class") // final이 아닌 타입도 저장할 수 있음, 객체의 클래스 정보를 저장 | ||
.registerModules(new JavaTimeModule()); // Java8의 날짜와 시간을 사용하기 위한 모듈을 등록 | ||
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer( | ||
objectMapper); | ||
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); | ||
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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/footprints/api/domain/items/repository/RedisItemRepository.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.footprints.api.domain.items.repository; | ||
|
||
import com.footprints.api.config.redis.BaseRedisRepository; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.Serializable; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.data.redis.core.ZSetOperations; | ||
import org.springframework.data.redis.core.ZSetOperations.TypedTuple; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class RedisItemRepository extends BaseRedisRepository implements Serializable { | ||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public static final String RANKING = "item_rankings"; // 채팅룸에 입장한 클라이언트의 sessionId와 채팅룸 id를 맵핑한 정보 저장 | ||
private ZSetOperations<String, String> zSetOperations; | ||
|
||
@PostConstruct | ||
protected void init() { | ||
classInstance = RedisItemRepository.class; | ||
zSetOperations = redisTemplate.opsForZSet(); | ||
} | ||
|
||
public List<TypedTuple<String>> getRankings(long start, long end) { | ||
return zSetOperations.rangeWithScores(RANKING, start, end).stream() | ||
.sorted(Comparator.comparingDouble(TypedTuple::getScore)) // Sort by score if needed | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
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