Spring 集成 Ehcache 开启缓存

1.jar包

1.1.slf4j-api-1.6.1.jar

1.2.ehcache-2.7.0.jar

1.3.spring-core-3.2.0.RELEASE.jar

1.4.spring-context-3.2.0.RELEASE.jar

2.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--磁盘存储:用来指定缓存在磁盘上的存储目录。
    可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)-->
    <diskStore path="d:/ehcache/"></diskStore>
    
    <!-- 默认缓存配置 -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="12000"
        overflowToDisk="true"
    />
    
    <!-- cache:为指定名称的对象进行缓存的特殊配置 -->
    <cache 
        name="CorpInfo" 
        maxElementsInMemory="10000" 
        eternal="false"
        timeToIdleSeconds="10000" 
        timeToLiveSeconds="10000" 
        overflowToDisk="true" 
    />

</ehcache>

 

 

3.springBean

<?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-3.2.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
 
  <description>ehcache缓存配置管理文件</description>

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

  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>

  <!-- 启用缓存注解开关 -->
  <cache:annotation-driven cache-manager="cacheManager"/>
 
</beans>

 

 

 

 

 

4.注解使用方法

4.1.@Cacheable

主要针对方法配置,能够根据方法的请求参数对其结果进行缓存。

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value=”menuCache”,key=”#userName”)
condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

例如:
@Cacheable(value=”menuCache”,

condition=”#userName.length()>2”)

 

 

 

 

 

 

 

 

 

 

 

4.2.@CachePut

  主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如:
@Cacheable(value=”menuCache”,

key=”#userName”)

condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

例如:
@Cacheable(value=”menuCache”,

condition=”#userName.length()>2”)

 

 

 

 

 

 

 

 

 

 
 

 

 

4.3.@CachEvict

主要针对方法配置,能够根据一定的条件对缓存进行清空。

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@CachEvict(value=” menuCache”) 或者 
@CachEvict(value={”cache1”,”cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如:
@CachEvict(value=” menuCache”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存 例如:
@CachEvict(value=” menuCache”,
condition=”#userName.length()>2”)
allEntries 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 例如:
@CachEvict(value=” menuCache”,allEntries=true)
beforeInvocation 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 例如:
@CachEvict(value=”menuCache”,beforeInvocation=true)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.案例

在 service 层实现类中,需要放入缓存的方法前添加注解

    @Override
    @Cacheable(value = "CorpInfo",key = "#cacheParam")
    public List<Ztree> getCorpZtreeList(String cacheParam) {
        List<CorpInfo> allCorpList = this.corpDao.findAll();
        List<Ztree> ztreeList = new ArrayList<Ztree>();
        
            /**
             * 处理逻辑代码
             * */

        return ztreeList;
    }                

 

posted @ 2017-02-21 14:33  MIC2016  阅读(3207)  评论(0编辑  收藏  举报