docker pull redis
docker run -d -t -p 4379:6379 --name redis redis --requirepass "{myPassword}"
docker exec -it redis /bin/bash //접속 완료
root@a770a65ae1b3:/data# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 8625
OK
127.0.0.1:6379> keys *
(empty array)
redis-cli
: redis를 명령어로 관리하도록 redis 내 접속auth
: redis 컨테이너를 실행할 때 넣어줬던 비밀번호로 인증을 거침keys *
: 모든 키 조회redis.yml
spring:
redis:
host: i9a603.p.ssafy.io
port: 4379
password: 8625
host
: host 주소 지정port
: port 지정password
: 암호 지정RedisConfig
package pokerface.pokerface.config.redis;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Getter
@Configuration
@RequiredArgsConstructor
@EnableRedisRepositories // Redis Repository 활성화
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
/**
* ec2내 Redis 컨테이너에 연결
*/
@Bean
public RedisConnectionFactory redisConnectionFactory(){
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host); // host 설정
redisStandaloneConfiguration.setPort(port); // port 설정
redisStandaloneConfiguration.setPassword(password); // password 설정
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
/**
* RedisConnection에서 넘겨준 byte 값 객체 직렬화
*/
@Bean
public RedisTemplate<?,?> redisTemplate(){
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
Room(redis에서 사용할 엔티티)
package pokerface.pokerface.domain.room.entity;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import pokerface.pokerface.domain.member.entity.Member;
import java.util.List;
@Getter
@Setter
@RedisHash(value = "room", timeToLive = 100000)
@NoArgsConstructor
@ToString
public class Room {
@Id
private String sessionId;
@Indexed
private String title;
@Indexed
private String gameMode;
private Boolean isPrivate;
private String roomPassword;
private List<Member> members;
@Builder
public Room(String sessionId, String gameMode, String title, Boolean isPrivate, String roomPassword, List<Member> members) {
this.sessionId = sessionId;
this.gameMode = gameMode;
this.title = title;
this.isPrivate = isPrivate;
this.roomPassword = roomPassword;
this.members = members;
}
}
@RedisHash(value = "room", timeToLive = 100000)
: redis에서 관리할 객체라는 걸 명시@Id
: 같은 key들 간 구분하는 식별자의 역할을 한다.@Indexed
: second key를 두어 조회를 더욱 빠르게 해준다.keys *
: 모든 키 조회