๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring Data Redis๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ ˆ๋””์Šค ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

by ํ‘์‹œ๋ฐ” 2024. 2. 12.

๐Ÿ‘€ ์†Œ๊ฐœ

Spring Data Redis๋Š” ๋ ˆ๋””์Šค์™€์˜ ํ†ตํ•ฉ์„ ์ง€์›ํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋ ˆ๋””์Šค๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค. ํ•ด๋‹น ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ ˆ๋””์Šค์˜ ๊ธฐ๋ณธ ๋ช…๋ น์–ด ๋“ค์„ Spring Data Redis๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.

์˜์กด์„ฑ ์ถ”๊ฐ€

๋จผ์ €, ํ”„๋กœ์ ํŠธ์— Spring Data Redis ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. Maven์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด pom.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

Gradle์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, build.gradle ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

Redis ์„ค์ •

application.properties ๋˜๋Š” application.yml ํŒŒ์ผ์— Redis ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

spring.redis.host=localhost
spring.redis.port=6379

RedisTemplate์„ ํ†ตํ•ด ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์‚ฌ์šฉํ•˜๊ธฐ

RedisTemplate์€ Redis์™€์˜ ์ƒํ˜ธ ์ž‘์šฉ์„ ์ถ”์ƒํ™”ํ•˜๊ณ  ์ผ๋ฐ˜์ ์ธ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค ์ž‘์—…์„ ๋‹จ์ˆœํ™”ํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

RedisTemplate์€ ๋ ˆ๋””์Šค ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” opsFor~() ๋กœ ์‹œ์ž‘๋˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ,์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ณ  ์ฝ๋Š” ์ž‘์—…, ์บ์‹œ ๊ตฌํ˜„, ์„ธ์…˜ ์ €์žฅ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๋ ˆ๋””์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ์ธ string ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณธ๋‹ค.String ๋ฐฉ์‹ ๊ฐ™์€ ๊ฒฝ์šฐ, opsForValue() ๊ฐ’์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

RedisStringService

@Service
@RequiredArgsConstructor
public class RedisStringService {

    private final RedisTemplate<String, String> redisTemplate;

    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    public void del(String key) {
        redisTemplate.delete(key);
    }
}

Test

@SpringBootTest
class RedisStringServiceTest {

    @Autowired
    private RedisStringService redisStringService;

    @Test
    void testSetAndGet() {
        // given
        String key = "BlackShiba";
        String value = "ShibaHolic";

        // when
        redisStringService.set(key, value);

        // then
        String result = redisStringService.get(key);
        assertThat(result).isEqualTo("ShibaHolic");
    }

    @Test
    void testDelete() {
        // given
        String key = "BlackShiba";
        String value = "ShibaHolic";
        redisStringService.set(key, value);

        // when
        redisStringService.del(key);

        // then
        String result = redisStringService.get(key);
        assertThat(result).isNull();
    }
}

 

 

Spring Data Redis๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ ๋ ˆ๋””์Šค ๋ช…๋ น์–ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค.

 

๋ณดํ†ต Redis๋ฅผ ์บ์‹ฑ/์„ธ์…˜์—์„œ ๋งŽ์ด ํ™œ์šฉํ•˜๋Š”๋ฐ, ๋ ˆ๋””์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋Ÿฐ ๊ธฐ๋ณธ ๋ช…๋ น์–ด๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ ์ ˆํžˆ ํ™œ์šฉํ•œ๋‹ค๋ฉด, ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ๋Ÿ‰ ๊ฐœ์„ ์— ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ž˜ ํ™œ์šฉํ•ด ๋ณด์ž.

๐Ÿ“– REFERENCE

https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTemplate.html

๋Œ“๊ธ€