curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
sudo service redis-server start
redis-cli
127.0.0.1:6379> ping
PONG
SET [keyName] [value] [값이 이미 있으면 실패:nx, 값이 이미 있으면 성공:xx]
GET [keyName]
INCR [keyName] [step(default 1)]
DECR [keyName] [step(default 1)]
MSET [key1] [value1] [key2] [value2] ...
MGET [key1] [key2] [key3] ...
EXIST [key1] [key2] ...
DEL
DEL [key1] [key2] ...
EXPIRE [keyName] 5(seconds)
해시화된 키 값에 field / value 쌍을 넣을 수 있음.
한개일때는 O(1), 늘어날수록 O(N)
HSET key field value [field value ...]
HGET key field
HMGET key field [field ...]
HGETALL key
HVALS key
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
Supported Feature | Lettuce | Jedis |
---|---|---|
독립형 연결 | X | X |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis:write-to-master-read-from-replica | X | |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis:sentinel | Master Lookup, Sentinel Authentication, Replica Reads | Master Lookup |
Redis 클러스터 | Cluster Connections, Cluster Node Connections, Replica Reads | Cluster Connections, Cluster Node Connections |
전송 채널 | TCP, OS-native TCP (epoll, kqueue), Unix Domain Sockets | TCP |
커넥션 풀 | X (using commons-pool2) | X (using commons-pool2) |
Other Connection Features | Singleton-connection sharing for non-blocking commands | Pipelining and Transactions mutually exclusive. Cannot use server/connection commands in pipeline/transactions. |
SSL 지원 | X | X |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#pubsub | X | X |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#pipeline | X | X (Pipelining and Transactions mutually exclusive) |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#tx | X | X (Pipelining and Transactions mutually exclusive) |
데이터 타입 | Key, String, List, Set, Sorted Set, Hash, Server, Stream, Scripting, Geo, HyperLogLog | Key, String, List, Set, Sorted Set, Hash, Server, Scripting, Geo, HyperLogLog |
https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis:reactive | X |
// 기본
@Configuration
public class RedisConfig {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
}
# application.yml
redis:
host: localhost # docker로 실행시 host.docker.internal
port: 6379
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedisConnectionFactory redisConnectionFactory(){
return new LettuceConnectionFactory(host, port);
}
@Bean
public RedisTemplate<?, ?> redisTemplate(){
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
@Repository
@RequiredArgsConstructor
public class RefreshTokenRepository {
// RedisTemplate<KeyType, ValueType>
private final RedisTemplate<String, String> redisTemplate;
public void saveRefreshToken(String memberId, String refreshToken) {
redisTemplate.opsForValue().set(memberId, refreshToken);
}
public String getRefreshToken(String memberId) {
return redisTemplate.opsForValue().get(memberId);
}
public void expire(String memberId, Long exp) {
redisTemplate.expire(memberId, exp, TimeUnit.MILLISECONDS);
}
}
Method | Description |
---|---|
opsForValue | Strings를 직렬화/ 역직렬화해주는 interface |
opsForList | List를 직렬화/ 역직렬화해주는 interface |
opsForSet | Set를 직렬화/ 역직렬화해주는 interface |
opsForZSet | ZSet를 직렬화/ 역직렬화해주는 interface |
opsForHash | Hash를 직렬화/ 역직렬화해주는 interface |
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RedisHash("member")
public class Member {
@Id
private String memberId;
private String email;
private String password;
// etc attributes...
}
public interface MemberRedisRepository extends CrudRepository<Member, String > {
}
@RequiredArgsConstructor
@Service
public class MemberService {
private final MemberRedisRepository memberRedisRepository;
public void addMember() {
Member member = Member.builder()
.id("1")
.email("zxc123@naver.com")
.password("1q2w3e4r5t")
.build();
memberRedisRepository.save(member);
}
}
※ 참조