前言
在 java 项目广泛的使用中它是一个开源的设计于提高在数据从 RDBMS 中取出来的高花费高延迟采取的一种缓存方案
正因为 Ehcache 具有健壮性 (基于 java 开发) 被认证 (具有 apache 2.0 license) 充满特色(稍后会详细介绍),
所以被用于大型复杂分布式 web application 的各个节点中
特点
1 够快
Ehcache 的发行有一段时长了, 经过几年的努力和不计其数的性能测试, Ehcache 终被设计于 large, high concurrency systems.
2 够简单
开发者提供的接口非常简单明了, 从 Ehcache 的搭建到运用运行仅仅需要的是你宝贵的几分钟其实很多开发者都不知道自己用在用 Ehcache,Ehcache 被广泛的运用于其他的开源项目
比如: hibernate
3 够袖珍
关于这点的特性, 官方给了一个很可爱的名字 small foot print , 一般 Ehcache 的发布版本不会到 2M,V 2.2.3 才 668KB
4 够轻量
核心程序仅仅依赖 slf4j 这一个包, 没有之一!
5 好扩展
Ehcache 提供了对大数据的内存和硬盘的存储, 最近版本允许多实例保存对象高灵活性提供 LRULFUFIFO 淘汰算法, 基础属性支持热配置支持的插件多
6 监听器
缓存管理器监听器 (CacheManagerListener)和 缓存监听器(CacheEvenListener), 做一些统计或数据一致性广播挺好用的
如何使用
Maven 依赖
- <!-- 加入缓存 -->
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>2.6.6</version>
- </dependency>
配置文件
在 resources 资源目录下创建一个 ehcache-config.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"
- updateCheck="false">
- <!-- EhCache 在每次启动的时候都要连接到 ehcache 网站上去检查新版本 使用如上的 updateCheck="false" 来禁止这个检查新版本 -->
- <!--
- name:cache 唯一标识
- eternal: 缓存是否永久有效
- maxElementsInMemory: 内存中最大缓存对象数
- overflowToDisk(true,false): 缓存对象达到最大数后, 将缓存写到硬盘中
- diskPersistent: 硬盘持久化
- timeToIdleSeconds: 缓存清除时间
- timeToLiveSeconds: 缓存存活时间
- diskExpiryThreadIntervalSeconds: 磁盘缓存的清理线程运行间隔
- memoryStoreEvictionPolicy: 缓存清空策略
- 1.FIFO:first in first out 先进先出
- 2.LFU: Less Frequently Used 一直以来最少被使用的
- 3.LRU:Least Recently Used 最近最少使用的
- -->
- <diskStore path="java.io.tmpdir/ehcache" />
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="FIFO" />
- <cache name="normal_cache"
- maxElementsInMemory="200"
- eternal="false"
- timeToIdleSeconds="7200"
- timeToLiveSeconds="7200"
- overflowToDisk="true"
- maxElementsOnDisk="1000"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="FIFO" />
- </ehcache>
spring 整合配置
注意以下内容必须注册在 spring 的主配置文件中
- <!-- 缓存配置文件接口 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
- <!-- 创建缓存管理器工厂 -->
- <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
- <property name="configLocation" value="classpath:ehcache-config.xml"></property>
- </bean>
- <!-- 创建缓存管理器 -->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
- <property name="cacheManager" ref="cacheManagerFactory"></property>
- </bean>
使用方法
这里可以使用注解的方式 @Cacheable(value = cache_pos_codes) 其中 value 的是设置的配置文件 ehcache-config.xml 的配置名称, 需要注意的是 import org.springframework.cache.annotation.Cacheable;
- @RequestMapping(value = "/date",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE + CHARSET)
- @ResponseBody
- @Cacheable(value = "cache_pos_codes")
- public String getDate(){
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return simpleDateFormat.format(new Date());
- }
来源: http://www.bubuko.com/infodetail-2540729.html