Chinaunix首页 | 论坛 | 博客
  • 博客访问: 27808
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-17 16:06
文章分类

全部博文(13)

文章存档

2017年(5)

2016年(8)

我的朋友

分类: Java

2017-02-20 19:19:32

ehcache是springframework中提供的缓存解决方案。用EhCache可以轻松实现对某些资源的内存缓存,并进行定时更新及手动更新。

Spring与EhCache结合,通过注解的方式即可简单实现某个方法返回值的缓存。在获取数据库数据或者其他IO成本高的数据时,缓存很有必要。

EhCache的接入过程如下:

1. pom中添加依赖:

点击(此处)折叠或打开

  1. <dependency>
  2. <groupId>net.sf.ehcache</groupId>
  3. <artifactId>ehcache</artifactId>
  4. <version>2.9.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-context-support</artifactId>
  9. <version>4.1.1.RELEASE</version>
  10. </dependency>

2. 在classpath下增加ehcache配置文件 ehcache.xml



  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache updateCheck="false">
  3. <diskStore path="java.io.tmpdir"/>
  4. <!--
  5. name:缓存名称。
  6. maxElementsInMemory:缓存最大个数。
  7. eternal:对象是否永久有效,一但设置了,timeout将不起作用。
  8. timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。
  9. 仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  10. timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。
  11. 仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
  12. overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
  13. diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
  14. maxElementsOnDisk:硬盘最大缓存个数。
  15. diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts
  16. of the Virtual Machine. The default value is false.
  17. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
  18. memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。
  19. 默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
  20. clearOnFlush:内存数量最大时是否清除。
  21. -->
  22. <defaultCache
  23. maxElementsInMemory="10000"
  24. eternal="false"
  25. timeToIdleSeconds="60"
  26. timeToLiveSeconds="60"
  27. overflowToDisk="true"
  28. maxElementsOnDisk="10000000"
  29. diskPersistent="false"
  30. diskExpiryThreadIntervalSeconds="120"
  31. memoryStoreEvictionPolicy="LRU"
  32. />
  33. <cache name="someDOCache" maxElementsInMemory="10000" eternal="false"
  34.        timeToLiveSeconds="1800" overflowToDisk="false" diskPersistent="false" />
  35. </ehcache>

3. spring配置文件 applicationContext.xml ,增加xmlns和xsi



点击(此处)折叠或打开

  1. <!-- ehcache -->
  2. <cache:annotation-driven cache-manager="ehcacheManager"/>
  3. <!-- 声明cacheManager -->
  4. <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  5. <property name="cacheManager" ref="ehcacheManagerFactory" />
  6. </bean>
  7. <!-- cacheManager工厂类,指定ehcache.xml的位置 -->
  8. <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  9. <property name="configLocation" value="classpath:util/ehcache.xml" />
  10. </bean>

4. 在想要将结果缓存的方法上加注解@Cacheable即可



  1. //此方法的返回值将进入someDOCache缓存起来,key为allRules
  2. @Cacheable(value = "someDOCache", key = "'allRules'")
  3. public List<someDO> getAllData() {
  4. //从数据库获取某数据
  5. }
  6.   
  7. //此方法清除指定key下的缓存内容,以便下次重新加载
  8. @CacheEvict(value = "someDOCache",key = "'allRules'")
  9. public void updateAllData() {
  10. }
  11.  
  12.  
  13. //key可以用入参拼接,如下(缓存了“hello{id}的字符串)
  14. @Cacheable(value = "dbCache",key = "'getMsg_' + #id")
  15. public String getMsg(String id) {
  16.     LOG.info("cache Msg(id) by " + id);
  17.     return "hello " + id;
  18. }
  19. @CacheEvict(value = "dbCache",key = "'getMsg_' + #id")
  20. public void updateMsg(String id) {
  21.     LOG.info("updateMsg by id " + id);
  22. }
  23.   
  24. //此外,可以指定condition,只有满足条件才会cache或evict
  25. @Cacheable(value = "QY_api_productTop",key="#isbn.id",condition = "#isbn.id<10")
  26. public Manual findManual(ISBN isbn, boolean checkWarehouse)

5. 注解详情

@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存

@Cacheable 支持如下几个参数:
value:缓存位置名称,不能为空,即ehcache.xml中声明的cache name
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL

@CacheEvict 支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false

一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值。

参考链接:

https://my.oschina.net/lemonzone2010/blog/405202

http://qincidong.github.io/blog/2015/03/24/spring-ehcache-annotation.html


阅读(737) | 评论(0) | 转发(0) |
0

上一篇:枚举类

下一篇:测试工具(junit, mockito)

给主人留下些什么吧!~~