본문 바로가기
Spring/DB

Ehcache 2 -> Ehcache 3 마이그레이션

by YoonJong 2023. 1. 23.
728x90

이전에 Ehcache2 로 적용해보았는데, 3으로 마이그레이션 해보았다.

이유는 버전 3이 적극적으로 유지 관리되고 개발된 버전이며, 유형이 안전하고 JSR107과 호환된다고 한다.

 

(JSR107 : Java 캐싱 API  https://github.com/jsr107/jsr107spec )

https://stackoverflow.com/questions/47163873/difference-relationship-between-ehcache-v2-and-ehcache-v3

 

Difference / Relationship between EhCache v2 and EhCache v3

Recently I am doing some research about the performance of java local caches and, of course, I run into ehcache. As far as can see in the documentation and in the artifacts published in maven repos...

stackoverflow.com


크게 다른점은 없었는데, Ehcache의 설정부분에서 조금의 차이가 있었다.

 

ehcache.xml 의 설정부분이며, resources의 아래 위치하게 두었다.

적용에 초점을 두어 만료시간은 10분로 해두었다. 

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <service>
        <jsr107:defaults enable-management="true" enable-statistics="true"/>
    </service>

    <!-- 환경설정 시작! -->
    <cache-template name="myDefaultTemplate">
        <!-- 만료시간 -->
        <expiry>
            <ttl unit="seconds">600</ttl>
        </expiry>
        <!-- 메모리 크기  -->
        <heap>20</heap>
    </cache-template>
    <!-- 캐시 이름 정의 -->
    <cache alias="goods" uses-template="myDefaultTemplate">
    </cache>

</config>

 

yml에 config 를 추가해준다.

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

 

사용방법은 이전과 같이 메서드에 설정해주면된다.

내가 적용할 곳은 상품 전체검색과 상품 상세보기(상세페이지같은) 곳에 적용했다.

// 상품 전체 검색
@Override
@TimerAop
@Transactional(readOnly = true)
@Cacheable(cacheNames = "goods", key = "#pageable")
public List<GoodsPageResponse> goodsFindAll(Pageable pageable) {
    Page<Goods> goods = goodsRepository.findAll(pageable);
    List<GoodsPageResponse> list = new ArrayList<>();

    List<GoodsPageResponse> goodsPageResponseList =
            goods.stream().map(good -> GoodsPageResponse.toResponse(good, goods)).collect(Collectors.toList());

    list.addAll(goodsPageResponseList);
    return list;
}
..
..

// 상품 상세(정보)조회
@Override
@TimerAop
@Transactional(readOnly = true)
@Cacheable(cacheNames = "goods", key = "#goodsId")
public GoodsResponse goodsDetailFind(Long goodsId) {
    Goods goods = goodsRepository.findById(goodsId).orElseThrow(
            () -> new BusinessException(ErrorCode.NOT_FOUND_GOODS));
    return GoodsResponse.toResponse(goods);
}

 

 

 

+++

1. 테스트 코드의 yml 파일이 따로 존재한다면, 캐시 또한 같이 적용해주어야 한다.

아래와 같은 오류 발생

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Cannot find cache named 'goods' for Builder[public java.util.List com.project.shop.goods.service.Impl.GoodsServiceImpl.goodsFindAll(org.springframework.data.domain.Pageable)] caches=[goods] | key='#pageable' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

 

 

2. 정상적으로 종료되는 로직에 사용해야 한다.

예외가 발생하는 로직에 사용하지 않는다.

728x90

댓글