본문 바로가기
SPRING/EHCACHE 설정

[ SPRING ] EHCACHE 를 annotation 으로 설정

by 정윤재 2012. 9. 6.

1. ehcache 를 annotation 으로 설정 하는 소스를 다운로드 함

 

http://code.google.com/p/ehcache-spring-annotations/

 

2. library 추가

 

3. applicationContext.xml (spring 설정 파일) 에 xmlns 와 xsi

 

추가

 

xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"


4. applicationContext.xml 의 beans 태그 안에 다음과 같이 추가 함


(주의 할 점은 아래 설정은 <context:component-scan base-package="com.incross" /> 밑에 있어야 한다는 것)

 

<ehcache:annotation-driven />
 
<ehcache:config cache-manager="cacheManager">
   <ehcache:evict-expired-elements interval="300" />
</ehcache:config>

<bean id="cacheManager" 
 class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation">
     <value>classpath:conf/cache/ehcache.xml</value>
   </property>
</bean>


5. 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"
  maxBytesLocalHeap="32M" maxBytesLocalOffHeap="128M"
  maxBytesLocalDisk="0M" updateCheck="false">
        
  <cache
    name="YunChaeCache" 
        maxElementsInMemory="200" 
        eternal="false" 
        overflowToDisk="false" 
        timeToLiveSeconds="30" />
    
</ehcache>

 

6. Service 에 cache 설정

 

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;
import com.googlecode.ehcache.annotations.When;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT ,rollbackFor = {Exception.class,SQLException.class} ,readOnly = false)
public class UserService {
 
 private static final Logger logger  =  Logger.getLogger(UserService.class);
 @Autowired
 public UserDAO userDAO;
 
 
 @Cacheable(cacheName = "YunChaeCache")
 public String selectUser(String USER_NM) throws SQLException{

  int user_id    =   0;
 
  user_id     =   userDAO.selectUSER_ID(USER_NM);
   


  return user_id+"";
 }

 
}

//캐쉬 삭제 하기 위한 설정은

 

@TriggersRemove(cacheName="YunChaeCache", removeAll=true, when=When.AFTER_METHOD_INVOCATION)

 

 

상세 내용은

 

http://code.google.com/p/ehcache-spring-annotations/w/list

 

를 참고 하면 됩니다.

 


댓글