spring
ehcache2(net.sf.ehcache)에서 ehcache3(org.ehcache)로 변경
moonsiri
2024. 4. 30. 13:15
728x90
반응형
Overview
Spring Boot 3.0에서는 ehcache2에 대한 지원이 제거되었습니다.
Jakarta EE 9 이상을 지원하기 위해 ehcache 및 ehcache-transactions 모듈에 대한 종속성 관리가 jakarta를 사용하여 선언되어 ehcache3로 버전을 업그레이드해야합니다.
Migration
1. Maven dependency 추가
V2)
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
V3)
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.10.8</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>3.2.5</version>
</dependency>
2. ehcache.xml 파일 생성 및 캐시 설정
V2)
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<cache
name="commonCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
V3)
ehcache.xml의 alias 태그와 @Cacheable의 value, ehcache.xml의 key-type 태그와 @Cacheable의 key, value-type 태그와 메소드의 반환형이 일치해야합니다.
<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="commonCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<ttl unit="minutes">5</ttl>
</expiry>
<heap unit="entries">100</heap>
</cache>
</config>
3. application.yml에 ehcache.xml 파일 경로 추가
V2)
spring:
cache:
ehcache:
config: classpath:cache/ehcache.xml
V3)
spring:
cache:
jcache:
config: classpath:cache/ehcache.xml
4. Java configuration
V2)
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableCaching
public class EhCacheConfiguration {
/**
* cache key 를 확인하기 위해 등록하는 bean
*
* @return
*/
@Bean
public EhCacheCacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
/**
* EhCacheCacheManager 객체가 참조하는 캐쉬 설정 파일 셋팅
*
* @return
*/
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
cmfb.setConfigLocation(new ClassPathResource("/cache/ehcache.xml"));
cmfb.setShared(true);
return cmfb;
}
}
V3)
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class EhCacheConfiguration {
}
5. 캐시를 사용할 Service 메소드에 어노테이션 추가
@Service
public class MyService {
@Cacheable(value = "commonCache", key = "#param")
public String getData(String param) {
return "Cached Data for " + param;
}
}
6. 캐시 조회
V2)
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCache;
@Resource
private CacheManager cacheManager;
public void clearEhCache() {
for (String cacheName: cacheManager.getCacheNames()) {
Objects.requireNonNull(cacheManager.getCache(cacheName)).clear();
}
}
public List<Map<String, String>> getEhCacheList() {
List<Map<String, String>> resultList = new ArrayList<>();
for (String cacheName : cacheManager.getCacheNames()) {
EhCacheCache cache = (EhCacheCache) cacheManager.getCache(cacheName);
Ehcache ehcache = cache.getNativeCache();
for (Object cacheKey : ehcache.getKeys()) {
Element element = ehcache.get(cacheKey);
if (element != null) {
resultList.add(Map.of(cacheName, element.toString()));
}
}
}
return resultList;
}
V3)
import org.springframework.cache.CacheManager;
import org.springframework.cache.jcache.JCacheCache;
import javax.cache.Cache;
@Resource
private CacheManager cacheManager;
public void clearEhCache() {
for (String cacheName: cacheManager.getCacheNames()) {
Objects.requireNonNull(cacheManager.getCache(cacheName)).clear();
}
}
public List<Map<String, String>> getEhCacheList() {
List<Map<String, String>> resultList = new ArrayList<>();
for (String cacheName : cacheManager.getCacheNames()) {
JCacheCache cache = (JCacheCache) cacheManager.getCache(cacheName);
if (cache == null) {
continue;
}
Cache<Object, Object> nativeCache = cache.getNativeCache();
for (Cache.Entry<Object, Object> entry : nativeCache) {
Object key = entry.getKey();
Object value = entry.getValue();
resultList.add(Map.of(key.toString(), value.toString()));
}
}
return resultList;
}
[Reference]
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#ehcache3
https://www.ehcache.org/documentation/3.10/getting-started.html
728x90
반응형