티스토리 뷰

jdk 7, egovframework 3.8 (spring 4.3)

1. pom.xml에 dependency 추가

<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache-core</artifactId>
	<version>2.6.9</version>
	<exclusions>
		<exclusion>
			<artifactId>slf4j-api</artifactId>
			<groupId>org.slf4j</groupId>
		</exclusion>
	</exclusions>
</dependency>

 

2. 캐시 설정 추가 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">
 
<defaultCache
	eternal="false"
	maxElementsInMemory="50"
	overflowToDisk="false"
	diskPersistent="false"
	timeToIdleSeconds="100"
	timeToLiveSeconds="200"
	memoryStoreEvictionPolicy="LRU" />
 
<cache
	name="globalMenuCache"
	eternal="false"
	maxElementsInMemory="100"
	overflowToDisk="false"
	diskPersistent="false"
	timeToIdleSeconds="0"
	timeToLiveSeconds="0"
	memoryStoreEvictionPolicy="LRU" />
 
</ehcache>
  • name : 캐시의 이름이다. @Cacheable("캐시의 이름") 와 일치시켜줘야한다. 
  • eternal : 한번 캐시하면 영원히 유지할 것인지의 여부 
  • maxElementsInMemory : 메모리에 보유할 최대 오브젝트 수 
  • overflowToDisk : 메모리저장공간이 부족할때 Disk 사용여부( true,false ) 
  • diskPersistent : flush시에 파일로 저장 여부 ( trun , false ) 
  • timeToIdleSeconds : 데이터가 지정된 시간(초단위)동안 재호출되지 않으면 휘발됨 
  • timeToLiveSeconds : 한번 저장된 데이터의 최대 저장 유지 시간(초단위) 
  • memoryStoreEvictionPolicy : FIFO-가장 먼저 적재된 페이지 교체, LFU-가장 적은 참조횟수를 갖는 페이지를 교체, LRU 가장 오랫동안 참조되지 않은 페이지를 교체

 

3. 캐시 서비스 추가

package egovframework.sample.menu.service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
@Service("sampleMenuService")
public class SampleMenuService {
	@Resource
	SampleMenuMapper sampleMenuMapper;
 
	@Cacheable(value="globalMenuCache")
	public List globalMenuList(HttpServletRequest request) throws Exception {
		return sampleMenuMapper.globalMenuList(pMap);
	}
}

 

3. context-cache.xml 추가

<?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:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	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/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
 
<cache:annotation-driven />
 
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
   <property name="cacheManager" ref="ehcache"/>
</bean>
 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation" value="classpath:egovframework/디렉토리/ehcache.xml"/>
</bean>
 
</beans>

 

 

참고: https://thereclub.tistory.com/10

 

 

 

끝.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함