spring +ehcache 注解方式配置缓存(spring与googlecode 的ehcache 两种)

前言:有人知道 -spring  +ehcache 注解方式和mybatis +ehcache的区别吗?

spring的ehcache②googlecode 的ehcache)//这是两种配置,都能实现ehcache缓存。绿色表示1需要,蓝色表示2需要 黑色表示都需要

google的ehcache配置更简单,它为spring与ehcache整合提供了封装包

一、导入所需要的包 ehcache-core-2.4.4.jar  ehcache-spring-annotations-1.2.0.jar guava-r09.jar(slf4j-api-1.6.2.jar和

   slf4j-log4j12-1.6.2.jar也要 <!--ehcache依赖slf4j--> <!--slf4j需要log4j-->

  googlecode 的ehcache必须依赖于ehcache-spring-annotations-1.2.0.jar guava-r09.jar

二、项目目录先看下:

三、新建 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">
    <!-- java.io.tmpdir:Java临时目录。指定一个文件目录,当EhCache把数据写到硬盘上或者系统jvm内存时,将把数据写到这个文件目录下 -->
    <diskStore path="java.io.tmpdir"/>


<!-- maxElementsInMemory:设置基于内存的缓存可存放对象的最大数目。  -->
<!-- eternal:如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false; -->
<!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了 -->
<!-- timeToIdleSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。 -->
<!-- 如果该属性值为0,则表示对象可以无限期地处于空闲状态。  -->
<!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。  -->
<!-- overflowToDisk:如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。 -->
    <!-- 设定缓存的默认数据过期策略 -->
    <defaultCache
            maxElementsInMemory="10000" 
            eternal="false" 
            overflowToDisk="true"
            timeToIdleSeconds="10"
            timeToLiveSeconds="20"
            diskPersistent="false"  
 diskExpiryThreadIntervalSeconds="120"/>
<!--  自定义缓存策略-学生信息缓存容器对应策略-->

    <cache name="userCache"          // 注解时候@Cacheable(value="userCache") 和这个name一致

扫描二维码关注公众号,回复: 4196299 查看本文章

                                                            @Cacheable(cacheName="userCache") 

        maxElementsInMemory="1000"  
        eternal="false"             
        overflowToDisk="true"       
        timeToIdleSeconds="10"      
        timeToLiveSeconds="20"/> 
   <!--  自定义缓存策略-教师信息缓存容器对应策略-->  
<!--      <cache name="techerCache"          -->
<!--         maxElementsInMemory="1000"  -->
<!--         eternal="false"                 -->
<!--         overflowToDisk="true"       -->
<!--         timeToIdleSeconds="10"      -->
<!--         timeToLiveSeconds="20"/>        -->

</ehcache>

四、新建 applicationContext-echache.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"  

  // 如果没上面这句会报错:元素 "cache:annotation-driven" 的前缀 "cache" 未绑定。

    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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/aop    
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
           http://www.springframework.org/schema/context    
           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
           http://www.springframework.org/schema/cache   
           http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

           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">  

   <!--spring的ehcache -->

<!--①的 缓存配置 -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
    <!--
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
            </set>
        </property>
    </bean>
     -->
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="/WEB-INF/ehcache.xml"/><!--//注意这个路径--> 
        <property name="shared" value="true"/> <!-- 这里是关键!!!没有必错  -->
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>

<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
    <!--
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
            </set>
        </property>
    </bean>
     -->
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="/WEB-INF/ehcache.xml"/><!--//注意这个路径--> 
        <property name="shared" value="true"/> <!-- 这里是关键!!!没有必错  -->
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>

    </bean>

<!--googlecode 的ehcache --> 

<ehcache:annotation-driven cache-manager="ehCacheManager" />   

   <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
       <property name="configLocation" value="/WEB-INF/ehcache.xml"/>     
    </bean>  


</beans>  

五、在spring-mvc.xml中导入applicationContext-echache.xml

     xml中加上  <!--加载ehcache缓存-->

     <import resource="applicationContext-echache.xml"/>  <!--//注意这个路径--> 

六、测试类:

*controoler.java:

@RequestMapping("findUserList")

public String findUserList(HttpServletRequest request, Model model) {
User user1 = (User) request.getSession().getAttribute("user");
List<User> user = userService.findUserList(user1.getName()); //第一次执行sql查询 在该方法上面配置如下
System.out.println("----------------------------start");
List<User> user22 = userService.findUserList(user1.getName()); //第二次执行缓存中获取
System.out.println("----------------------------end");
model.addAttribute("userList", user);
return "user/userInformation";
}

*service.java

import org.springframework.cache.annotation.Cacheable;

import com.googlecode.ehcache.annotations.Cacheable;

@Cacheable(value="userCache")    和ehcache中<cache name="userCache"  相同

@Cacheable(cacheName="userCache") 

public List<User> findUserList(String name) {
// TODO Auto-generated method stub
return iUserMapper.findUserList(name);

}

七、执行打印日志如下

 2018-06-14 21:00:39,354 [http-bio-8080-exec-4] DEBUG [com.user.dao.IUserMapper.findUserList] - ==>  Preparing: select a.* from users a where a.name not in (?) //第一次执行
 2018-06-14 21:00:39,354 [http-bio-8080-exec-4] DEBUG [com.user.dao.IUserMapper.findUserList] - ==> Parameters: 2(String)
 2018-06-14 21:00:39,358 [http-bio-8080-exec-4] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1241c3f]
 ----------------------------start//第二次执行

----------------------------end

八、在摸索阶段自己犯了很多错误,一直不能成功。现在虽然成功,但是还有很多关于它的疑问。难受难受!!!

猜你喜欢

转载自blog.csdn.net/sinat_34338162/article/details/80698018