ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] Ehcache 사용
    카테고리 없음 2020. 9. 18. 10:24

    개발환경

    Spring 5.2.7.RELEASE


     

    [캐쉬 서비스 동작원리]

     

    기존 서비스 : Controller > Service > Dao 

    캐쉬 서비스 : Controller > Service > CacheService (> Dao)

     

    캐쉬를 통해 메뉴 DB 제어

     


    Dependency 추가

    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>${org.springframework-version}</version>
    			<exclusions>
    				<!-- Exclude Commons Logging in favor of SLF4j -->
    				<exclusion>
    					<groupId>commons-logging</groupId>
    					<artifactId>commons-logging</artifactId>
    				 </exclusion>
    			</exclusions>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>net.sf.ehcache</groupId>
    			<artifactId>ehcache</artifactId>
    			<version>2.10.6</version>
    		</dependency>

     

    Cache-servlet.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: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"
    	xmlns:p="http://www.springframework.org/schema/p">
    	<cache:annotation-driven />
    	
    	<!-- annotation 기반 캐시 스캔 -->
    	<bean id="cacheManager"
    		class="org.springframework.cache.ehcache.EhCacheCacheManager"
    		p:cache-manager-ref="ehcache" />
    	<bean id="ehcache"
    		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    		p:config-location="/WEB-INF/config/ehcache.xml" p:shared="true" />
    </beans>

     

    ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
    	monitoring="autodetect" dynamicConfig="true">
    
    	<diskStore path="java.io.tmpdir" />
    
    	<cache name="menuCache" maxElementsInMemory="1000"
    		eternal="true" overflowToDisk="false" timeToLiveSeconds="60"
    		diskPersistent="false" memoryStoreEvictionPolicy="LRU" />
    </ehcache>

    이때 캐쉬 bean id는 CacheService.java에서 메소드에 사용될 CacheName에 대한 맵핑 설정

    설정 부분 참고사이트

    hyeooona825.tistory.com/86

     

    [Spring] Ehcache 캐시 사용

    캐시 적용을 왜 해야 하는거지? 컴퓨터 전공자라면, 컴퓨터 관련 과목을 수강해 본 적이 있으시다면 '캐시(Cache)' 라는 말을 들어보신 적이 있을 것입니다. 캐시라고 구글링 해보시면 많은 자료가

    hyeooona825.tistory.com

     

    Ehcahce에서 MenuVo Interceptor

    Config 생성

    src > webapp > WEB-INF > config >

      cache-servlet.xml

      ehcache.xml 

     

    캐쉬서비스 생성

    comm > service > CacheService.java

     

    CacheServie.java

    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    
    import com.donggyu.project.comm.dao.CommDao;
    import com.donggyu.project.comm.vo.menuVo;
    import com.sun.istack.internal.logging.Logger;
    
    @Service
    public class CacheService {
    	@Resource(name = "commDao")
    	private CommDao commDao;
    
    	Logger log = Logger.getLogger(this.getClass());
    
    	@SuppressWarnings("unchecked")
    	@Cacheable(value="menuCache")
    	public List<menuVo> sltCacheMenuList() throws Exception{
    
    		log.info("cache start");
    		List<menuVo> menuList = null;
    
    		menuList = commDao.menuList();
    
    		return menuList;
    	}
    }
    

     

    인터셉터 PostHandel 설정

    	@Override
    	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
    			ModelAndView modelAndView) throws Exception {
    		log.info("====================================== END ======================================\n");
    		modelAndView.addObject(commService.sltMenuList());
    		super.postHandle(request, response, handler, modelAndView);
    	}

     

    결과화면

    댓글

Designed by Tistory.