Redis를 사용하여 Cache 관리하기

Redis and cache

EhCache 는 thread 내의 cache 전용이여서 분산시스템환경에서 사용하기에는 적합하지 않다. 물론 EhCache 자체도 분산시스템을 지원하지만 캐쉬가 동기화 되지 않는 경우 많다.

아래에 Srpingboot 내부에서 지원한 redis cache 를 설정하고 사용해보자.

동일하게 지난번 사례 코드를 그대로 적용해보자

@Entity
@Data
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

interface 더 만든다.

pom.xml 에 dependency를 추가한다.

주의! springboot 1.x 에서 명칭은 spring-boot-starter-redis 로 되어 있다. 잘 구분 하자!

application.properties

application.yml

Redis conncetion pool 설정 관련 하여 주의해야 될 부분은 springboot1.x 에서 spring.redis.pool 를 사용한다.

1.x 에서는 jedis 를 사용하고

2.x 에서는 lettuce 를 사용한다.

테스트 코드를 작성

로그에서 찍힌 대로

  • CacheManager type : class org.springframework.data.redis.cache.RedisCacheManager

  • 두번째 조회시 SQL 이 수행되지 않았다.

끝!

Last updated