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;
}

Spring 集成 Ehcache 开启缓存的更多相关文章

  1. spring集成ehcache本地缓存

    1.maven依赖 <!-- ehcache 相关依赖 --> <dependency> <groupId>net.sf.ehcache</groupId&g ...

  2. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  3. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

  6. spring整合ehcache实现缓存

    Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便. spring本身内置了对Cache的支持,之 ...

  7. Hibernate 集成 Ehcache 开启二级缓存

    一.将 Ehcache.xml 放到 classpath 下 <?xml version="1.0" encoding="UTF-8"?> < ...

  8. 二)spring 集成 ehcache jgroups 集群

    依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-co ...

  9. spring-data-redis集成ehcache实现缓存

    1.结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns=&qu ...

随机推荐

  1. python上安装requests

    首先需要配置好python的环境变量. 测试是否成功配置,进入命令行,输入python. 如下图为配置成功. 去第三方库的网站下载安装包,解压在python的安装目录 ,下载地址:https://py ...

  2. 转载:Spring学习总结

    地址:http://www.cnblogs.com/best/tag/Spring/

  3. 关于符号Symbol第二篇

    来看一下继承自Symbol的具体实现类. 1.TypeSymbol /** A class for type symbols. * Type variables are represented by ...

  4. 使用python遍历文件夹取出特定的字符串

    # -*- coding: utf-8 -* import re import os # 需要处理的文件夹路径(绝对路径) path = u"/Users/a140/Downloads/te ...

  5. Chapter 3 Phenomenon——3

    It took every ounce of my concentration to make it down the icy brick driveway alive. 我用所有我的注意力去确定车道 ...

  6. 机器学习中的范数规则化之L0、L1与L2范数

    今天看到一篇讲机器学习范数规则化的文章,讲得特别好,记录学习一下.原博客地址(http://blog.csdn.net/zouxy09). 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...

  7. 根据模板导出excel

    @RequestMapping(value = "/export", method = RequestMethod.GET) public void exportApprovals ...

  8. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

  9. mysql 用存储过程和函数分别模拟序列

    在其他大部分DBMS里都有序列的概念,即Sequence或Generator. 而mysql里没有,但有时真的很有用.下面分别用存储过程和函数来模拟序列,并用程序模拟并发场景来测试原子性和完整性,是否 ...

  10. 哪个类可用于处理 Unicode?

    A. InputStreanReader的构造函数: InputStreamReader(InputStream in)          创建一个使用默认字符集的 InputStreamReader ...