Spring์์ ์ง์ํ๋ Spring Data Redis๋ฅผ ํตํด์ Redis์ DB์ ์ฐ๋ํ ์ ์๋ค. Spring Data Redis๋ฅผ ํ์ฉํ๋ฉด ์ค์ ๊ณผ Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๊ฒ์ ์์ฝ๊ฒ ํ ์ ์๋ค.
Redis๋ฅผ Window์ ์ค์นํ๋ ๊ณผ์ ๊ณผ Redis ์๋ฃ๊ตฌ์กฐ์ ๋ช ๋ น์ด๋ ๋ค๋ฅธ ๊ธ์ ์์ผ๋ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.

Spring Data Redis๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด `Redis 2.6` ์ด์์ ๋ฒ์ ์ด ํ์ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด ๊ธ์ `Spring Boot 3.2` ๋ฒ์ + `Java 17`์ ๊ธฐ์ค์ผ๋ก ์์ฑ๋์๋ค. + `Lombok` ์ถ๊ฐ
์์กด์ฑ ์ถ๊ฐ
Spring Initiaizer๋ก ์์ํ๋ค๋ฉด `Spring Data Redis(Access+Driver)`๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
`build.gradle`์ `dependencies`์ `spring-boot-starter-data-redis`๋ฅผ ์ถ๊ฐํด์ค๋ค.
ํธ์คํธ์ ํฌํธ๋ฒํธ ์ค์
application.properties
spring.data.redis.host=localhost
spring.data.redis.port=6379
application.yml
spring:
data:
redis:
host: localhost
port: 6379
โ Spring Boot 3.2 ๋ฒ์ ์์๋ ๊ธฐ์กด์ `spring.redis.host` ๊ฐ Deprecated ๋์ด์ ์ธ ์๊ฐ ์๋ค. `spring.data.redis.host`๋ก ์ ์ด์ค์ผ ํ๋ค!
`host`์ `port`๋ฅผ ์ค์ ํ ์ ์๋ค.
`password`๋ `pool` ๋ฑ๋ ์ถ๊ฐ๋ก ์ค์ ํ ์ ์๋ค.
Configuration ์ถ๊ฐ
Spring Data Redis๋ Redis DB์์ ์ฐ๊ฒฐ์ ์ํด ์๋ฐ Redis ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Lettuce์ Jedis๋ฅผ ํ์ฉํ๋ค.
Lettuce๋ ๋น๋๊ธฐ๋ก ๋์ํด Jedis์ ๋นํด ์ฑ๋ฅ์ด ์ข๊ธฐ๋ ํ๊ณ , Lettuce ์ฌ์ฉ์ ๊ถ์ฅํ๋ค๊ณ ํ๋ค.
@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);
}
}
Redis์ ์ค์ ์ ๋ณด๋ฅผ Configuration Class์์ ์ฃผ์ ํ๋ค. `application.yml`์์ ์ค์ ํ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
`RedisConnectionFactory`๋ Redis DB์ ์ฐ๊ฒฐํ๋ ์ญํ ์ ํ๋ค.
Redis ๋ฐ์ดํฐ ์ ๊ทผํ๊ธฐ
Spring Data Redis๊ฐ ์ ๊ณตํ๋ Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์๋ `RedisTemplate` ๊ณผ `RedisRepositories`๊ฐ ์๋ค.
RedisTemplate
RedisTemplate ์ Operations ์ ์ ๊ณตํด Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ค.
Configuration์ ์ถ๊ฐ
@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);
}
//RedisTemplate ์ฌ์ฉ์ ์ํ ์ถ๊ฐ
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}
Configuration Class์ `RedisTemplate` ์ค์ ์ ์ถ๊ฐํ๋ค.
RedisTemplate ์ฌ์ฉ
RedisTemplate ์ ์๋ฃ๊ตฌ์กฐ ๋ณ Operations ๋ฅผ ์ ๊ณตํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตฌํ๋ ๋ฉ์๋๋ก Redis ๋ช ๋ น์ด๋ฅผ ์ํํ๋ฉด ๋๋ค.
@SpringBootTest
class RedisTemplateTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void redisTemplateString() {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String key = "name";
valueOperations.set(key, "giraffe");
String value = valueOperations.get(key);
Assertions.assertEquals(value, "giraffe");
}
}
- `ValueOperations` : String ์ ๊ณต ํด๋์ค
- `ListOperations` : List ์ ๊ณต ํด๋์ค
- `SetOperations` : Set ์ ๊ณต ํด๋์ค
- `ZSetOperations` : Sorted Set ์ ๊ณต ํด๋์ค
- `HashOperations` : Hash ์ ๊ณต ํด๋์ค
Redis Repositories
Redis Repositories๋ ๋งคํ๋ Domain Object๋ฅผ Redis์ Hashes๋ก ๋ณํํ์ฌ ์ ์ฅํ๋ค. Spring Data JPA์์ Repository๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋น์ทํ๋ค.
`Redis 2.8.0` ์ด์์ ๋ฒ์ ์์ ์ฌ์ฉํ ์ ์๊ณ , ํธ๋์ ์ ์ ์ง์ํ์ง ์์ผ๋ฏ๋ก RedisTemplate๊ณผ ์จ์ผ ํ๋ค.
Entity
@Getter
@RedisHash("people")
public class Person {
@Id
private String id;
private String firstname;
private String lastname;
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
- `@RedisHash` ์ ๊ฐ์ด Set์ key๋ก ๋ค์ด๊ฐ๊ณ , value(member)์๋ `@Id` ์ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
(id๊ฐ์ ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด null๋ก, ๋๋ค๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.)
- `member:id๊ฐ`์ Hash์ด๊ณ , key, value์๋ Entity์ ํ๋์ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
Repository
public interface PersonRepository extends CrudRepository<Person, String> {
}
๊ธฐ๋ณธ์ ์ธ CRUD๋ฅผ ์ ๊ณตํ๋ `CrudRepository`๋ฅผ ์์๋ฐ๋๋ค.
์ฌ์ฉ
@SpringBootTest
public class RedisRepositoryTest {
@Autowired
private PersonRepository personRepository;
@Test
void test() {
Person person = new Person("Giraffe", "Kim");
personRepository.save(person);
Person person2 = new Person("turtle", "Kim");
personRepository.save(person2);
personRepository.findById(person.getId());
// personRepository.delete(person);
}
}
- `save()` : id๊ฐ null์ด๋ผ๋ฉด ์๋ก์ด id๋ฅผ ์์ฑํ๊ณ , key๊ฐ `keyspace:id` (people:id๊ฐ)ํจํด์ผ๋ก ์ ์ฅ๋๊ณ , Redis์ Hash๋ก ๊ฐ์ฒด(Person)๊ฐ ์ ์ฅ๋๋ค.
- `findById()` : keyspace:id ์ ํด๋น๋๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
- `count()` : `@RedisHash`๋ก ์ ์ํ keyspace(people)์ ์ ์ฅ๋ Enitity(Person)์ ๊ฐ์๋ฅผ ๊ฐ์ ธ์จ๋ค.
- `delete()` : ํด๋น ๊ฐ์ฒด(person)์ ์ญ์ ํ๋ค.
๊ฒฐ๊ณผ


์ฐธ๊ณ
- Spring Data Redis ๊ณต์ ๋ฌธ์ : https://docs.spring.io/spring-data/redis/reference/
- Lettuce VS Jedis : https://akku-dev.tistory.com/105
- Spring Boot ์์ Redis ์ฌ์ฉํ๊ธฐ : https://bcp0109.tistory.com/328
- Redis Repository ๊ณต์ ๋ฌธ์ : https://docs.spring.io/spring-data/redis/reference/repositories.html
'Backend > Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] Spring Boot 3๊ณผ Spring Boot 2 ๋น๊ต (0) | 2024.03.19 |
---|---|
[Spring Boot] Redis๋ก ๋ญํน ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ (0) | 2024.02.09 |
[Spring Cloud] Eureka Registered instance ์ ๊ฑฐํ๊ธฐ (0) | 2023.05.12 |
[Spring] Spring Boot์ QueryDSL ๋์ ๊ธฐ (0) | 2023.04.13 |
[Spring] TypeError: Failed to execute 'fetch' on 'Window' : Request with GET/HEAD method cannot have body. (0) | 2023.03.26 |
Spring์์ ์ง์ํ๋ Spring Data Redis๋ฅผ ํตํด์ Redis์ DB์ ์ฐ๋ํ ์ ์๋ค. Spring Data Redis๋ฅผ ํ์ฉํ๋ฉด ์ค์ ๊ณผ Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๊ฒ์ ์์ฝ๊ฒ ํ ์ ์๋ค.
Redis๋ฅผ Window์ ์ค์นํ๋ ๊ณผ์ ๊ณผ Redis ์๋ฃ๊ตฌ์กฐ์ ๋ช ๋ น์ด๋ ๋ค๋ฅธ ๊ธ์ ์์ผ๋ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.

Spring Data Redis๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Redis 2.6
์ด์์ ๋ฒ์ ์ด ํ์ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด ๊ธ์ Spring Boot 3.2
๋ฒ์ + Java 17
์ ๊ธฐ์ค์ผ๋ก ์์ฑ๋์๋ค. + Lombok
์ถ๊ฐ
์์กด์ฑ ์ถ๊ฐ
Spring Initiaizer๋ก ์์ํ๋ค๋ฉด Spring Data Redis(Access+Driver)
๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
build.gradle
์ dependencies
์ spring-boot-starter-data-redis
๋ฅผ ์ถ๊ฐํด์ค๋ค.
ํธ์คํธ์ ํฌํธ๋ฒํธ ์ค์
application.properties
spring.data.redis.host=localhost
spring.data.redis.port=6379
application.yml
spring:
data:
redis:
host: localhost
port: 6379
โ Spring Boot 3.2 ๋ฒ์ ์์๋ ๊ธฐ์กด์ spring.redis.host
๊ฐ Deprecated ๋์ด์ ์ธ ์๊ฐ ์๋ค. spring.data.redis.host
๋ก ์ ์ด์ค์ผ ํ๋ค!
host
์ port
๋ฅผ ์ค์ ํ ์ ์๋ค.
password
๋ pool
๋ฑ๋ ์ถ๊ฐ๋ก ์ค์ ํ ์ ์๋ค.
Configuration ์ถ๊ฐ
Spring Data Redis๋ Redis DB์์ ์ฐ๊ฒฐ์ ์ํด ์๋ฐ Redis ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Lettuce์ Jedis๋ฅผ ํ์ฉํ๋ค.
Lettuce๋ ๋น๋๊ธฐ๋ก ๋์ํด Jedis์ ๋นํด ์ฑ๋ฅ์ด ์ข๊ธฐ๋ ํ๊ณ , Lettuce ์ฌ์ฉ์ ๊ถ์ฅํ๋ค๊ณ ํ๋ค.
@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);
}
}
Redis์ ์ค์ ์ ๋ณด๋ฅผ Configuration Class์์ ์ฃผ์
ํ๋ค. application.yml
์์ ์ค์ ํ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
RedisConnectionFactory
๋ Redis DB์ ์ฐ๊ฒฐํ๋ ์ญํ ์ ํ๋ค.
Redis ๋ฐ์ดํฐ ์ ๊ทผํ๊ธฐ
Spring Data Redis๊ฐ ์ ๊ณตํ๋ Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์๋ RedisTemplate
๊ณผ RedisRepositories
๊ฐ ์๋ค.
RedisTemplate
RedisTemplate ์ Operations ์ ์ ๊ณตํด Redis์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ค.
Configuration์ ์ถ๊ฐ
@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);
}
//RedisTemplate ์ฌ์ฉ์ ์ํ ์ถ๊ฐ
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}
Configuration Class์ RedisTemplate
์ค์ ์ ์ถ๊ฐํ๋ค.
RedisTemplate ์ฌ์ฉ
RedisTemplate ์ ์๋ฃ๊ตฌ์กฐ ๋ณ Operations ๋ฅผ ์ ๊ณตํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ตฌํ๋ ๋ฉ์๋๋ก Redis ๋ช ๋ น์ด๋ฅผ ์ํํ๋ฉด ๋๋ค.
@SpringBootTest
class RedisTemplateTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void redisTemplateString() {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String key = "name";
valueOperations.set(key, "giraffe");
String value = valueOperations.get(key);
Assertions.assertEquals(value, "giraffe");
}
}
ValueOperations
: String ์ ๊ณต ํด๋์คListOperations
: List ์ ๊ณต ํด๋์คSetOperations
: Set ์ ๊ณต ํด๋์คZSetOperations
: Sorted Set ์ ๊ณต ํด๋์คHashOperations
: Hash ์ ๊ณต ํด๋์ค
Redis Repositories
Redis Repositories๋ ๋งคํ๋ Domain Object๋ฅผ Redis์ Hashes๋ก ๋ณํํ์ฌ ์ ์ฅํ๋ค. Spring Data JPA์์ Repository๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋น์ทํ๋ค.
Redis 2.8.0
์ด์์ ๋ฒ์ ์์ ์ฌ์ฉํ ์ ์๊ณ , ํธ๋์ ์
์ ์ง์ํ์ง ์์ผ๋ฏ๋ก RedisTemplate๊ณผ ์จ์ผ ํ๋ค.
Entity
@Getter
@RedisHash("people")
public class Person {
@Id
private String id;
private String firstname;
private String lastname;
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
@RedisHash
์ ๊ฐ์ด Set์ key๋ก ๋ค์ด๊ฐ๊ณ , value(member)์๋@Id
์ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
(id๊ฐ์ ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด null๋ก, ๋๋ค๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.)
member:id๊ฐ
์ Hash์ด๊ณ , key, value์๋ Entity์ ํ๋์ ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
Repository
public interface PersonRepository extends CrudRepository<Person, String> {
}
๊ธฐ๋ณธ์ ์ธ CRUD๋ฅผ ์ ๊ณตํ๋ CrudRepository
๋ฅผ ์์๋ฐ๋๋ค.
์ฌ์ฉ
@SpringBootTest
public class RedisRepositoryTest {
@Autowired
private PersonRepository personRepository;
@Test
void test() {
Person person = new Person("Giraffe", "Kim");
personRepository.save(person);
Person person2 = new Person("turtle", "Kim");
personRepository.save(person2);
personRepository.findById(person.getId());
// personRepository.delete(person);
}
}
save()
: id๊ฐ null์ด๋ผ๋ฉด ์๋ก์ด id๋ฅผ ์์ฑํ๊ณ , key๊ฐkeyspace:id
(people:id๊ฐ)ํจํด์ผ๋ก ์ ์ฅ๋๊ณ , Redis์ Hash๋ก ๊ฐ์ฒด(Person)๊ฐ ์ ์ฅ๋๋ค.findById()
: keyspace:id ์ ํด๋น๋๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.count()
:@RedisHash
๋ก ์ ์ํ keyspace(people)์ ์ ์ฅ๋ Enitity(Person)์ ๊ฐ์๋ฅผ ๊ฐ์ ธ์จ๋ค.delete()
: ํด๋น ๊ฐ์ฒด(person)์ ์ญ์ ํ๋ค.
๊ฒฐ๊ณผ


์ฐธ๊ณ
- Spring Data Redis ๊ณต์ ๋ฌธ์ : https://docs.spring.io/spring-data/redis/reference/
- Lettuce VS Jedis : https://akku-dev.tistory.com/105
- Spring Boot ์์ Redis ์ฌ์ฉํ๊ธฐ : https://bcp0109.tistory.com/328
- Redis Repository ๊ณต์ ๋ฌธ์ : https://docs.spring.io/spring-data/redis/reference/repositories.html
'Backend > Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] Spring Boot 3๊ณผ Spring Boot 2 ๋น๊ต (0) | 2024.03.19 |
---|---|
[Spring Boot] Redis๋ก ๋ญํน ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ (0) | 2024.02.09 |
[Spring Cloud] Eureka Registered instance ์ ๊ฑฐํ๊ธฐ (0) | 2023.05.12 |
[Spring] Spring Boot์ QueryDSL ๋์ ๊ธฐ (0) | 2023.04.13 |
[Spring] TypeError: Failed to execute 'fetch' on 'Window' : Request with GET/HEAD method cannot have body. (0) | 2023.03.26 |