1. pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<!-- Spring 혹은 Terasoluna 에 최신버전이 내장되어 있어서 버전을 지정하지 않았었음. 아마 2.10.x 버전인듯 -->
</dependency>
2. ehcache.xml
<?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">
<!--
* 속성 설명 (출처: https://javacan.tistory.com/entry/133)
- maxElementsInMemory: 메모리에 저장될 수 있는 객체의 최대 개수 (필수)
- eternal: 이 값이 true이면 timeout 관련 설정은 무시되고, Element가 캐시에서 삭제되지 않는다. (필수)
- overflowToDisk: 메모리에 저장된 객체 개수가 maxElementsInMemory에서 지정한 값에 다다를 경우 디스크에 오버플로우 되는 객체는 저장할 지의 여부를 지정한다. (필수)
- timeToIdleSeconds: Element가 지정한 시간 동안 사용(조회)되지 않으면 캐시에서 제거된다. 이 값이 0인 경우 조회 관련 만료 시간을 지정하지 않는다. 기본값은 0이다. (선택)
- timeToLiveSeconds: Element가 존재하는 시간. 이 시간이 지나면 캐시에서 제거된다. 이 시간이 0이면 만료 시간을 지정하지 않는다. 기본값은 0이다. (선택)
- diskPersistent: VM이 재 가동할 때 디스크 저장소에 캐싱된 객체를 저장할지의 여부를 지정한다. 기본값은 false이다. (선택)
- diskExpiryThreadIntervalSeconds: Disk Expiry 쓰레드의 수행 시간 간격을 초 단위로 지정한다. 기본값은 120 이다. (선택)
- memoryStoreEvictionPolicy: 객체의 개수가 maxElementsInMemory에 도달했을 때, 메모리에서 객체를 어떻게 제거할 지에 대한 정책을 지정한다. 기본값은 LRU이다. FIFO와 LFU도 지정할 수 있다. (선택)
-->
<!-- 기본 설정(필수) -->
<defaultCache
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<!-- SharedService.findTranslationOutput() -->
<cache name="translationCache"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
/>
</ehcache>
3. application-context.xml
- xmlns 에 cache 항목이 추가된 것에 주의
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 기타 등등 -->
<!--// 기타 등등 -->
<!--=========================== 캐시 관련 설정 - start ===========================-->
<cache:annotation-driven /><!-- 반드시 필요 -->
<!-- cacheManager 설정 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<!-- ehcache 설정 -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:META-INF/spring/ehcache.xml" />
</bean>
<!--=========================== 캐시 관련 설정 - end =============================-->
</beans>
4. Spring autowired scope 내 method 별 적용
/**
* 한글 용어를 외국어로 변환
*/
@Override
@Cacheable(value = "translationCache") // 특별히 key를 구성하지 않을 경우 메서드의 모든 인자의 조합이 키가 된다.
public String findTranslationOutput(String type, String ko, String lang) {
// 이런저런 코드
}