spring 3.x整合ehcache 2.x

2 篇文章 0 订阅
2 篇文章 0 订阅

1.pom.xml

        <!-- spring 相关的jar-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations -->
        <dependency>
            <groupId>com.googlecode.ehcache-spring-annotations</groupId>
            <artifactId>ehcache-spring-annotations</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.2</version>
        </dependency>

2.applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache" 
    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.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
        http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.2.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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!--other etc-->
    <!-- 缓存配置 -->  
    <ehcache:annotation-driven /> 
    <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"/> 
    </bean>
</beans>

3.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true"
    monitoring="autodetect" 
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>  
    <defaultCache  
           maxElementsInMemory="1000"  
           eternal="false"  
           timeToIdleSeconds="120"  
           timeToLiveSeconds="120"  
           overflowToDisk="false"/>  
    <cache name="myCache"  
           maxElementsOnDisk="20000"  
           maxElementsInMemory="2000"  
           eternal="true"  
           overflowToDisk="true"  
           diskPersistent="true"/>  
</ehcache> 

4.代码:

import com.googlecode.ehcache.annotations.Cacheable;

@Service
public class ChannelServiceImple implements ChannelService {
    @Cacheable(cacheName="myCache")
    @Override
    public List<ChannelCategory> getChannelCategories() {//Q5
        // TODO Auto-generated method stub
        List<ChannelCategory> ccs=Collections.emptyList();
        log.info("[apobates][cache start]service:getChannelCategories");
        //ETC
        log.info("[apobates][cache finish]service:getChannelCategories");
        return ccs;
    }
}

5.看看log日志,缓存是否起作用了 ,
之前(没有ehcache):

 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates][cache start]service:getChannelCategories
 [ INFO ]  [apobates][cache finish]service:getChannelCategories
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates][cache start]service:getChannelCategories
 [ INFO ]  [apobates][cache finish]service:getChannelCategories
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates][cache start]service:getChannelCategories
 [ INFO ]  [apobates][cache finish]service:getChannelCategories
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates][cache start]service:getChannelCategories
 [ INFO ]  [apobates][cache finish]service:getChannelCategories

global channel category是spring mvc controller输出的

@Controller
@Scope("prototype")
@RequestMapping(value="/channel")
@SessionAttributes({"channelForm"})
public class ChannelController {
    @Autowired
    private ChannelService channelService;
    private static final Logger log=Logger.getLogger(ChannelController.class);

    //GLcategory
    @ModelAttribute
    public void addInitAttr(Model model,HttpServletRequest request){
        log.info("[apobates]global channel category");
        model.addAttribute("GLcategory",channelService.getChannelCategories());
    }

当前():

 [ INFO ]  FrameworkServlet 'apobates_client': initialization completed in 213 ms
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates][cache start]service:getChannelCategories
 [ INFO ]  [apobates][cache finish]service:getChannelCategories
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates]global channel category
 [ INFO ]  [apobates]user channel category
 [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:19
 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:19
 [ INFO ]  [apobates]global channel category
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值