为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO。下面说明一下ehcache和spring整合配置。

1.   需要的jar包

slf4j-api-1.6.1.jar

ehcache-core-2.1.0.jar

ehcache-spring-annotations-1.1.2.jar(可选)

slf4j-log4j12-1.6.1.jar(可选)

spring-context-support-4.0.6.RELEASE.jar

2.   ehcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache updateCheck="false" name="defaultCache">
  3. <diskStore path="java.io.tmpdir/ehcache"/>
  4. <!-- 默认缓存 -->
  5. <defaultCache
  6. maxElementsInMemory="1000"
  7. eternal="false"
  8. timeToIdleSeconds="120"
  9. timeToLiveSeconds="120"
  10. overflowToDisk="false"/>
  11. <!-- 菜单缓存 -->
  12. <cache name="menuCache"
  13. maxElementsInMemory="1000"
  14. eternal="false"
  15. timeToIdleSeconds="120"
  16. timeToLiveSeconds="120"
  17. overflowToDisk="false"
  18. memoryStoreEvictionPolicy="LRU"/>
  19. </ehcache>

参数说明:

<diskStore>:当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)。

<diskStore path="">:用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index。

name:缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)。

maxElementsOnDisk:磁盘缓存中最多可以存放的元素数量,0表示无穷大。

maxElementsInMemory:内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况。

1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中。

2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素。

Eternal:缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds。

timeToIdleSeconds:缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除。

timeToLiveSeconds:缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大,即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除。

overflowToDisk:内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中),会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data。

diskPersistent:是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件,这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存,要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法。

diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔,默认是120秒。

diskSpoolBufferSizeMB:设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB

memoryStoreEvictionPolicy:内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存,共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)。

3.   application_spring_cache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:cache="http://www.springframework.org/schema/cache"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/cache
  11. http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
  12. <cache:annotation-driven cache-manager="cacheManager"/>
  13. <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  14. <property name="configLocation" value="classpath:application/ehcache.xml" />
  15. </bean>
  16. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  17. <property name="cacheManager"  ref="cacheManagerFactory"/>
  18. </bean>
  19. </beans>

4.   使用

首先在ehcache.xml中配置缓存策略,即添加一组cache。

4.1     添加缓存

  1. @Cacheable(value="menuCache",key="'UserMenuKey'+#userid")
  2. public List<MenuBean> queryMenuByUserId(String userid)
  3. {
  4. ……
  5. }

@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”)

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

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:
@Cacheable(value=” menuCache”) 或者
@Cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

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

key=”#userName”)

condition

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

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

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

4.2     清除缓存

  1. @CacheEvict(value="menuCache",key="'UserMenuKey'+#userRoleBean.getUserId()")
  2. public boolean delUserRole(UserRoleBean userRoleBean) {
  3. ……
  4. }

@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)

最新文章

  1. 在MVC架构中使用CodeSmith生成NHibernate映射对象和实体类
  2. CryptoJS遇到的小坑
  3. log4net 记录到数据库和本地文件
  4. 缺少索引导致的服务器和MYSQL故障。
  5. android 入门-生命周期 activity
  6. mysql注入小测试
  7. Why Python is Slow
  8. angular.js 字符串
  9. Android 视图切换库的使用 - SwitichLayout
  10. ionic开发android app步骤
  11. 关于arm-linux-gcc的安装与配置
  12. Android应用开发基础篇(1)-----Button
  13. MySQL高级查询(一)
  14. Matlab:如何查找给定目录下的文件
  15. 有趣的if循环
  16. 20155324 《Java程序设计》实验四 Android开发基础
  17. node_01_自定义模块(先创建package.json)
  18. meta标签使用方法总结
  19. vue elementui 写的一个后台管理页面模板
  20. 你需要了解 Windows Phone 8.1 的11件事

热门文章

  1. spring boot 整合 (全)
  2. java ee7 软件安装和环境配置
  3. 机器学习入门-数值特征-数字映射和one-hot编码 1.LabelEncoder(进行数据自编码) 2.map(进行字典的数字编码映射) 3.OnehotEncoder(进行one-hot编码) 4.pd.get_dummies(直接对特征进行one-hot编码)
  4. Mysql 表名大写 找不到表
  5. as3 air 获取文件夹下的所有文件
  6. Windows窗体技术
  7. kafka相关资料
  8. Packed with amazing data about the world in 201
  9. SVM支持向量机推导,工具介绍及python实现
  10. Linux CentOS7中 设置IP地址、网关DNS