IT/Live Coding

Spring Boot + Redis ์บ์‹ฑ ๊ตฌํ˜„ํ•˜๊ธฐ (ํ…Œ์ŠคํŠธ ์˜์ƒ & ์†Œ์Šค ์ฝ”๋“œ ํฌํ•จ)

์–ดํฅ๊ผฌ๋น„ 2025. 3. 5.

Redis๋ฅผ ํ™œ์šฉํ•œ ์บ์‹ฑ ๊ตฌํ˜„! (์งง๊ณ  ๊ฐ„๋‹จํ•œ ์˜์ƒ)

์—ญ์‹œ๋‚˜ ๋ ˆ๋””์Šค ๊ณต๋ถ€ํ•œ ๊ฒƒ ๊นŒ๋จน๊ธฐ ์‹ซ์–ด ๊ธฐ๋ก์œผ๋กœ ๋‚จ๊ธด๋‹ค.

๋™์˜์ƒ

์†Œ์Šค

application.yml

server:
  port: 9090
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true
      force: true
spring:
  cache:
    type: redis
  devtools:
    livereload.enabled: true
    restart.enabled: true
  datasource:
    driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
    url: jdbc:log4jdbc:mysql://localhost:3309/db๋ช…
    hikari:
      username: ์‚ฌ์šฉ์ž
      password: ๋น„๋ฐ€๋ฒˆํ˜ธ
      connectionTimeout: 10000
      maximum-pool-size: 15
      max-lifetime: 600000
      readOnly: false
      connection-test-query: SELECT 1
  jpa:
    hibernate:
      ddl-auto: create #create update none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true

  # Redis ์„ธ์…˜ ์„ค์ •
  session:
    store-type: redis   # ์„ธ์…˜์„ Redis์— ์ €์žฅ
    redis:
      namespace: session   # Redis์— ์ €์žฅํ•  ์„ธ์…˜ ๋ฐ์ดํ„ฐ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค
      timeout: 1800  # ์„ธ์…˜ ํƒ€์ž„์•„์›ƒ (์ดˆ ๋‹จ์œ„)

# Redis ์„ค์ • ์ถ”๊ฐ€
data:
  redis:
    host: localhost   # Redis ์„œ๋ฒ„ ์ฃผ์†Œ
    port: 6379        # Redis ํฌํŠธ
    password:         # (ํ•„์š”ํ•˜๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ถ”๊ฐ€)
    timeout: 6000     # ์—ฐ๊ฒฐ ํƒ€์ž„์•„์›ƒ (ms)
    lettuce:
      pool:
        max-active: 8  # ์ตœ๋Œ€ ํ™œ์„ฑ ์—ฐ๊ฒฐ ์ˆ˜
        max-idle: 8    # ์ตœ๋Œ€ ์œ ํœด ์—ฐ๊ฒฐ ์ˆ˜
        min-idle: 0    # ์ตœ์†Œ ์œ ํœด ์—ฐ๊ฒฐ ์ˆ˜
        max-wait: -1   # ์ตœ๋Œ€ ๋Œ€๊ธฐ ์‹œ๊ฐ„ (๋ฌด์ œํ•œ)

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.9'
    id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.lsy'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    // Logging
    implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
    // redis
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    // swagger
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
    // cache
    implementation 'org.springframework.boot:spring-boot-starter-cache'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

RedisConfig

package com.lsy.rediscashing.user.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisConfig {

    @Value("${data.redis.host}")
    private String host;
    @Value("${data.redis.port}")
    private int port;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(host, port);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}

controller

package com.lsy.rediscashing.user.controller;

import com.lsy.rediscashing.user.model.User;
import com.lsy.rediscashing.user.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService service;

    public UserController(@Qualifier("userService1") UserService service) {
        this.service = service;
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return service.getUserData(id);
    }
}

service

package com.lsy.rediscashing.user.service;

import com.lsy.rediscashing.user.model.User;
import com.lsy.rediscashing.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service("userService1")
@Slf4j
@RequiredArgsConstructor
public class UserService {

    private final UserRepository repository;
    private final RedisTemplate redisTemplate;

    public User getUserData(Long id) {

        long startTime = System.currentTimeMillis();
        String cacheKey = "user:" + id;

        // ์บ์‹œ๊ฐ’ ์กฐํšŒ
        User cachedData = (User) redisTemplate.opsForValue().get(cacheKey);
        // ์บ์‹œ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ผ€์‹œ๊ฐ’ ๋ฐ˜ํ™˜
        if (cachedData != null) {
            log.info("redis data exist~");
            long endTime = System.currentTimeMillis(); // ๋ ์‹œ๊ฐ„ ์ธก์ •
            long elapsedTime = endTime - startTime; // ์‹œ๊ฐ„ ์ฐจ์ด (๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„)
            log.info("Cache retrieval time: {} ms", elapsedTime);
            return cachedData;
        }

        // ์บ์‹œ๊ฐ’์ด ์—†์œผ๋ฉด db์—์„œ ์กฐํšŒ
        log.info("redis data not exist~");
        long dbStartTime = System.currentTimeMillis(); // DB ์กฐํšŒ ์‹œ์ž‘ ์‹œ๊ฐ„
        User findUser = repository.findById(id).orElse(null);
        // db์กฐํšŒ ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋ ˆ๋””์Šค์— key, value ์ €์žฅ(๋งŒ๋ฃŒ์‹œ๊ฐ„์€ 400์œผ๋กœ)
        if (findUser != null) {
            redisTemplate.opsForValue().set(cacheKey, findUser, 400, TimeUnit.SECONDS);
        }

        long dbEndTime = System.currentTimeMillis(); // DB ์กฐํšŒ ๋ ์‹œ๊ฐ„
        long dbElapsedTime = dbEndTime - dbStartTime; // DB ์กฐํšŒ ์‹œ๊ฐ„
        long endTime = System.currentTimeMillis(); // ์ „์ฒด ์ข…๋ฃŒ ์‹œ๊ฐ„
        long totalElapsedTime = endTime - startTime; // ์ „์ฒด ์‹œ๊ฐ„

        log.info("DB retrieval time: {} ms", dbElapsedTime);
        log.info("Total time: {} ms", totalElapsedTime);

        return findUser;
    }
}

repository

package com.lsy.rediscashing.user.repository;

import com.lsy.rediscashing.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

}

User

package com.lsy.rediscashing.user.model;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.sql.Timestamp;

@Entity
@Table(name = "user_test")
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    @CreationTimestamp
    private Timestamp created_at;

    @Builder
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }
}

๊ฐœ์ธ ์Šคํ„ฐ๋”” ๊ธฐ๋ก์„ ๋ฉ”๋ชจํ•˜๋Š” ๊ณต๊ฐ„์ด๋ผ ํ‹€๋ฆฐ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ‹€๋ฆฐ ์  ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

Spring Boot + Redis๋กœ ์„ธ์…˜ ๊ณต์œ ํ•˜๊ธฐ (ํ…Œ์ŠคํŠธ ์˜์ƒ & ์†Œ์Šค ์ฝ”๋“œ ํฌํ•จ)

Redis๋ฅผ ํ™œ์šฉํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ„ ์„ธ์…˜ ๊ณต์œ  ์‹คํ—˜! (์งง๊ณ  ๊ฐ„๋‹จํ•œ ์˜์ƒ)๋ ˆ๋””์Šค๋ฅผ ๊ณต๋ถ€ํ•œ์ง€ ์–ผ๋งˆ ์•ˆ๋˜์–ด์„œ ๊นŒ๋จน๊ธฐ ์‹ซ์–ด ๊ธฐ๋ก์œผ๋กœ ๋‚จ๊ธด๋‹ค.๋™์˜์ƒ์†Œ์Šคapplication.ymlserver: port: 9090 servlet: context-path: / encodin

yaga.tistory.com

 

Spring Boot + Redis๋กœ ์‹ค์‹œ๊ฐ„ ๋žญํ‚น ๊ตฌํ˜„ (ํ…Œ์ŠคํŠธ ์˜์ƒ & ์†Œ์Šค ์ฝ”๋“œ ํฌํ•จ)

Redis๋ฅผ ํ™œ์šฉํ•œ ์‹ค์‹œ๊ฐ„ ๋žญํ‚น ๊ตฌํ˜„! (์งง๊ณ  ๊ฐ„๋‹จํ•œ ์˜์ƒ)์—ญ์‹œ๋‚˜ ๋ ˆ๋””์Šค ๊ณต๋ถ€ํ•œ ๊ฒƒ ๊นŒ๋จน๊ธฐ ์‹ซ์–ด ๊ธฐ๋ก์œผ๋กœ ๋‚จ๊ธด๋‹ค.๋™์˜์ƒ์†Œ์Šคapplication.ymlserver: port: 8081 servlet: context-path: / encoding: charset: UTF-8 enabled: true

yaga.tistory.com

 

๋Œ“๊ธ€