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)์„ ์‚ญ์ œํ•œ๋‹ค.

 

 

 

 

 

๊ฒฐ๊ณผ

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฐธ๊ณ 

giraffe_