아무거나

Spring Boot Ehcache3 Multiple CacheManager 적용 본문

Java & Kotlin/Spring

Spring Boot Ehcache3 Multiple CacheManager 적용

전봉근 2023. 7. 28. 11:44
반응형
  • Ehcache3 (Maven)
    • CacheManagerBean 구현을 통한 Ehcache3 적용
      • Ehcache3 부터는 JSR-107 로 CacheManager 로 구현해야 한다고 한다.
      • JSR-107 호환 캐시를 사용하기 위해선 JCache 구현 필요
      • 구현
        • ehcache.xml 생성 (resources 경로 - alias: @Cacheable 의 value 값, key: 키값, value-type: cache 로 저장할 값)
          <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-3.0.xsd
                      http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
          
              <cache alias="foo">
                  <key-type>java.lang.String</key-type>
                  <value-type>java.util.ArrayList</value-type>
                  <expiry>
                      <ttl unit="minutes">30</ttl>
                  </expiry>
          
                  <listeners>
                      <listener>
                          <class>com.lotteon.display.config.ehcache.CacheEventLogger</class>
                          <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                          <event-ordering-mode>UNORDERED</event-ordering-mode>
                          <events-to-fire-on>CREATED</events-to-fire-on>
                          <events-to-fire-on>EXPIRED</events-to-fire-on>
                      </listener>
                  </listeners>
          
                  <resources>
                      <heap unit="entries">10000</heap>
                      <offheap unit="MB">1000</offheap>
                  </resources>
              </cache>
          
          </config>        
          
        • Cache Logger
          [CacheEventLogger.java]
          ...
          
          import org.ehcache.event.CacheEvent;
          import org.ehcache.event.CacheEventListener;
          
          import lombok.extern.slf4j.Slf4j;
          
          @Slf4j
          public class CacheEventLogger
              implements CacheEventListener<Object, Object> {
          
              @Override
              public void onEvent(
                  CacheEvent<? extends Object, ? extends Object> cacheEvent) {
                  log.info(">>>>> key={}, old={}, new={}", cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
              }
          }
          
        • CacheManager 작성 [RedisConfig.java]
          // 그 외 캐시매니저들..
          ...
          
          // Ehcache3 CacheManager
          @Bean
          public JCacheManagerFactoryBean jCacheManagerFactoryBean() throws Exception{
              JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
              jCacheManagerFactoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
              return jCacheManagerFactoryBean;
          }
          @Bean
          public JCacheCacheManager ehCacheCacheManager(JCacheManagerFactoryBean jCacheManagerFactoryBean) {
              JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
              jCacheCacheManager.setCacheManager(jCacheManagerFactoryBean.getObject());
              return jCacheCacheManager;
          }      
          
          ...
          
          

 

참고

- https://www.ehcache.org/

- https://kok202.tistory.com/

- https://docs.spring.io/

반응형
Comments