MyBatis整合Ehcache详细介绍
1、初识Ehcache
Ehcache是一种广泛使用的Java分布式缓存器,具有快速、精干等特点,是Hibernate中默认CacheProvider。它提供内存存储和磁盘存储两种方案,因此无需担心容量问题。Ehcache可以单独使用,一般在第三方库中被用到的比较多,如Hibernate、MyBatis等
因为Ehcache直接在JVM虚拟机中缓存,速度快,效率高,但是缓存共享十分麻烦,集群分布式应用不方便。Ehcache是通过RMI或者Jgroup多播方式进行广播缓存通知更新,缓存共享复杂,维护不方便。虽然Ehcache号称“分布式缓存器”,实际来看,口宣能力大于实际能力,当然,从商业角度来说,这也无可厚非。
2、Ehcache入门例子
2.1、在pom.xml中引入依赖
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.10.2</version>
- </dependency>
2.2、在src/main/resources/创建一个配置文件 ehcache.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">
-
- <!-- 磁盘缓存位置 -->
- <diskStore path="java.io.tmpdir/ehcache"/>
-
- <!-- 默认缓存 -->
- <defaultCache
- maxEntriesLocalHeap="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- maxEntriesLocalDisk="10000000"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU">
- <persistence strategy="localTempSwap"/>
- </defaultCache>
-
- <!—自定义缓存:MyCache -->
- <cache name="MyCache"
- maxElementsInMemory="1000"
- eternal="false"
- timeToIdleSeconds="5"
- timeToLiveSeconds="5"
- overflowToDisk="false"
- memoryStoreEvictionPolicy="LRU"/>
- </ehcache>
2.3、测试类
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
-
- public class CacheTest {
- public static void main(String[] args) {
- // 1. 创建缓存管理器
- CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");
-
- // 2. 获取缓存对象
- Cache cache = cacheManager.getCache("HelloWorldCache");
-
- // 3. 创建元素
- Element element = new Element("key1", "value1");
-
- // 4. 将元素添加到缓存
- cache.put(element);
-
- // 5. 获取缓存
- Element value = cache.get("key1");
- System.out.println("value: " + value);
- System.out.println(value.getObjectValue());
-
- // 6. 删除元素
- cache.remove("key1");
-
- // 7. 刷新缓存
- cache.flush();
-
- // 8. 关闭缓存管理器
- cacheManager.shutdown();
-
- }
- }
3、MyBatis整合Ehcache
与Hibernate一样,MyBatis同样提供了一级缓存和二级缓存的支持。
一级缓存: 基于HashMap本地缓存,其存储作用域为Session,当Session flush或close之后,该Session中的所有Cache就将清空。
二级缓存:与一级缓存其机制相同,默认也是采用HashMap存储,不同在于其存储作用域为 Mapper,并且可自定义第三方存储源,如Ehcache框架等。
MyBatis中一级缓存是默认开启的,只要当SqlSession不关闭,那么你的操作会默认存储使用一级缓存。
对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Mapper)的进行了 Create/Updat/Delet 操作后,默认该作用域下所有select中的缓存将被clear。
3.1、引入Ehcache相关的包
MyBatis提供了一个cache接口,如果要实现自己的缓存逻辑,实现cache接口开发即可。对于MyBatis和Ehcache整合,可以直接使用开源的mybatis-ehcache,此整合包中提供了一个cache接口的实现类。在项目中加入如下两个jar包:
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.10.1</version>
- </dependency>
-
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-ehcache</artifactId>
- <version>1.0.0</version>
- </dependency>
3.2、配置mapper中cachetype
配置mapper中cache中的type为ehcache对cache接口的实现类型。
- <mapper namespace='UserMapper'>
-
- <cachetype='org.mybatis.caches.ehcache.EhcacheCache'>
-
- </cache>
3.3、根据需求调整缓存参数:
- <cachetype='org.mybatis.caches.ehcache.EhcacheCache'>
-
- <propertyname='timeToIdleSeconds'value='3600'/>
-
- <propertyname='timeToLiveSeconds'value='3600'/>
-
- <!-- 同ehcache参数maxElementsInMemory-->
-
- <propertyname='maxEntriesLocalHeap'value='1000'/>
-
- <!-- 同ehcache参数maxElementsOnDisk -->
-
- <propertyname='maxEntriesLocalDisk'value='10000000'/>
-
- <propertyname='memoryStoreEvictionPolicy'value='LRU'/>
-
- </cache>
3.4、加入Ehcache的配置文件
在classpath下配置ehcache.xml
- <ehcachexmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
-
- xsi:noNamespaceSchemaLocation='../config/ehcache.xsd'>
-
- <diskStorepath='/cn/mybatis/cache'/>
-
- <defaultCache
-
- maxElementsInMemory='1000'
-
- maxElementsOnDisk='10000000'
-
- eternal='false'
-
- overflowToDisk='false'
-
- timeToIdleSeconds='120'
-
- timeToLiveSeconds='120'
-
- diskExpiryThreadIntervalSeconds='120'
-
- memoryStoreEvictionPolicy='LRU'>
-
- </defaultCache>
-
- </ehcache>