dispatcher-Servlet.xml 설정. 또는 applicationContext.xml

* <context:component-scan ...> 바로 뒤에 <ehcache:annotation-driven ..> 와야 한다고 한다.

<?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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">

  <context:annotation-config />

  <context:component-scan base-package="com.samsung.mcm.master" />
  <!-- Ehcache -->
  <ehcache:annotation-driven cache-manager="cacheManager" />
  <ehcache:config cache-manager="cacheManager">
    <ehcache:evict-expired-elements
      interval="60" />
  </ehcache:config>

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

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">
  <defaultCache eternal="false" maxElementsInMemory="100"
    overflowToDisk="false" />
  <cache name="countCache"
    maxElementsInMemory="10"
    eternal="false"
    overflowToDisk="false"
    diskPersistent="false"
    timeToIdleSeconds="0" 
    timeToLiveSeconds="60"
    memoryStoreEvictionPolicy="LRU" />
    
</ehcache>

annotation 설정

@Cacheable(cacheName = "countCache")
 public List<TestObject> getCount() {
    return testMapper.selectCount();
  }

ref: 

http://ehcache.org

http://javacan.tistory.com/123

http://shonm.tistory.com/m/post/view/id/437


Posted by 뚜벅이조
,