我们先思考一下,什么是ehcache?ehcache能有否给我们带来方便,我们经常在哪里会用到它?当我们携带着这些问题之后,接下来,在好奇心的驱使下,我们可以完美进入学习ehcache的步调当中。
ehcache的优点:
1、简单、快速
2、缓存数据有两级:内存和磁盘,因此无需担心容量问题
3、缓存数据会在虚拟机重启的过程中写入磁盘
4、可以通过RMI、可插入API等方式进行分布式缓存
5、具有缓存和缓存管理的侦听接口
6、支持多缓存管理器实例,以及一个实例的多个缓存区域
7、提供Hibernate的缓存实现
8、多种缓存策略,Ehcache提供了对大数据的内存和硬盘存储,最近版本允许多实例、保存对象高灵活 性、提供LRU、LFU、FIFO淘汰算法,基础属性支持热部署、支持的插件多
9、传递的信息以键值对的形式存储
每件事物都是对立统一的,有这么多的优点,必然也会有其缺点。
ehcache的缺点:
1、使用磁盘Cache的时候非常占用磁盘空间:这是因为DiskCache的算法简单,该算法简单也导致Cache的 效率非常高。它只是对元素直接追加存储。因此搜索元素的时候非常的快。如果使用DiskCache,遇到 应用很频繁的操作,很快磁盘就会满。
2、不能保证数据的安全性,当突然kill掉java的时候,可能会产生冲突,Ehcache的解决方法是如何文件冲 突了,则重建cache,这可能不利于cache的保存,cache只是简单的加速,但是无法保证数据的安全。
如果想了解更多细节,此处跳转ehcache官方指南:https://www.ehcache.org/documentation/3.6/
下面讲解一下ehcache的实现方式(主要是配置CacheManager和Cache两个类)
一、编程式实现
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
cacheManager.init();
Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);
Cache<Long, String> myCache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)));
myCache.put(1L, "da one!");
String value = myCache.get(1L);
cacheManager.removeCache("preConfigured");
cacheManager.close();
二、XML配置实现
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
<cache alias="foo">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">20</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>
<cache alias="simpleCache" uses-template="myDefaults" />
</config>
使用的时候,就是通过xml解析,获取到manager,如下方展示:
URL myUrl = getClass().getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
如果各位看官是集成了是spring,将配置写入到applicationContext.xml配置文件,然后交给spring帮我们创建实例,如下方展示:
<!-- 缓存配置 -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<bean id="cacheManagerFactory" 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="cacheManagerFactory"/>
</bean>
cache配置详解:
name:Cache的唯一标识
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
eternal:Element是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会 Element写到磁盘中
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选 属性,默认值是0,也就是可闲置时间无穷大
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅 当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
diskPersistent:是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个 Cache都应该有自己的一个缓冲区
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去 清理内存。默认策略是LRU。一共有三种策略:
(1)、FIFO:先进先出
(2)、LFU:一直以来最少被使用的,缓存的元素有一个hit 属性,hit 值最 小的将会被清出缓存
(3)、LRU:近期最少使用的,缓存的元素有一个时间戳,当缓存容量满 了, 而又须要腾出地方来缓存新的元素的时候,那么现有缓存 元素中时间戳离当前时间最远的元素将被清出缓存
关于ehcache的基础知识大致讲解到这里,之后会给各位讲解一下ehcache的高级属性以及其他的各种技术