您的位置:首页 > 其它

mybatis--缓存(一级和二级缓存)

2018-06-29 10:58 387 查看
1.缓存

查询数据时将查询结果存放到内存(缓存区)中。

每次查询数据时,先判断缓存区中是否存在数据,

如果存在,就从缓存区中获取数据

如果不存在,就从数据库中获取数据,将数据存放到缓存区中,给下次访问使用

好处:

避免频繁与数据库交互,提高数据访问效率。提升系统性能。

缓存的使用演示:

失败的一级缓存测试 ※面试相关

[1]SQL语句或查询条件不同

[2]分属不同SqlSession对象

[3]查询前执行clearCache()

[4]提交事务

2.一级缓存

一级缓存是SqlSession自带的。SqlSession对象被创建,一级缓存就存在了。

如果SqlSession对象关闭或调用清理方法,会导致缓存失效。

缓存底层实现就是通过HashMap实现的。

一级缓存介质——内存

3.二级缓存

二级缓存介质——内存,硬盘

二级缓存SqlSessionFactory进行管理的。SqlSessionFactory对象是进程级别的。可以被多个SqlSession所共享。

跟Web应用中application对象作用范围类似。

MyBatis框架自带了二级缓存,是通过HashMap实现的。

二级缓存使用,需要在主文件中进行配置:

①启用二级缓存

<!-- 启用二级缓存 -->
<setting name="cacheEnabled" value="true"/>
②在XxxMapper.xml文件中配置二级缓存策略

<cache eviction="FIFO" flushInterval="60000"readOnly="true" size="512"/>

eviction:缓存策略

FIFO:First In First Out

LRU:Least Recently Used

flushInterval:缓存刷新时间间隔,时间是毫秒,检查是否存在过期对象

size:缓存中存储的对象个数

readOnly:是否只读

③对于MyBatis自带的二级缓存,实体类可以不用实现可序列化接口。

如果使用的是EhCahce缓存组件,有时会将数据缓存到硬盘上,需要可序列化支持的。必须实现java.io.Serializable

4.外置二级缓存组件

ehcache缓存组件。

实体类必须实现可序列化接口,否则,抛异常。

java.io.NotSerializableException: com.atguigu.mybatis.bean.Customer EnhancerByCGLIBEnhancerByCGLIB
eb28a8d2

①为什么要整合EHCache?

EHCache更专业,支持内存和硬盘缓存数据。

②操作

[1]添加jar包

ehcache-core-2.6.8.jar

mybatis-ehcache-1.0.3.jar

slf4j-api-1.6.1.jar

[2]加入EHCache自身配置文件ehcache.xml

<ehcache>
<!--设置硬盘缓存目录:内存中存储不下就可以往这个目录下存储了。会自动生成数据文件-->
<diskStore path="D:/temp"/>
<defaultCache<!--默认的缓存配置-->
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="20"
timeToLiveSeconds="120"
overflowToDisk="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
属性说明:
* diskStore:指定数据在磁盘中的存储位置。
* defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略

以下属性是必须的:
* maxElementsInMemory - 在内存中缓存的element的最大数目
* maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
* eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
* overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上

以下属性是可选的:
* timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
* timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
* diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
* diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每隔120s,相应的线程会进行一次EhCache中数据的清理工作
* memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
[3]开启EHCache缓存XxxMapper.xml文件中配置

EhcacheCache实现了Cache接口。

<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

[4]测试

openSession = sqlSessionFactory.openSession();
CustomerMapper mapper = openSession.getMapper(CustomerMapper.class);

Customer customer = mapper.getCustomerByCustId(1);
System.out.println(customer.getCustName());
customer = mapper.getCustomerByCustId(2);
System.out.println(customer.getCustName());

openSession.close();

openSession = sqlSessionFactory.openSession();
mapper = openSession.getMapper(CustomerMapper.class);

customer = mapper.getCustomerByCustId(1);
System.out.println(customer.getCustName());
5.二级缓存的原理:

Mybatis框架提供了Cache接口,缓存组件实现接口。

PerpetualCache : 框架默认二级缓存实现类。使用HashMap实现二级缓存的。

6.二级缓存失效情况

[1]在查询select标签内设置useCache="false"

<selectid="getStuById" parameterType="Integer"resultType="Student" useCache="false">

selectstu_id stuId,stu_name stuName from tbl_student where stu_id=#{stuId}

</select>

[2]在执行增删改操作时刷新缓存

<updateid="updateStu"parameterType="com.atguigu.mybatis.entity.Student" flushCache="true">

updatetbl_student set stu_name=#{stuName} where stu_id=#{stuId}

</update>

这是默认设置,通常不必修改

7.缓存命中率

从缓存中查找数据的几率

AAA
[DEBUG] 2017-04-08 10:57:04,342(1036) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.5
AAA
[DEBUG] 2017-04-08 10:57:04,342(1036) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.6
AAA
[DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.6666666666666666
AAA
[DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.7142857142857143
AAA
[DEBUG] 2017-04-08 10:57:04,343(1037) --> [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(LoggingCache.java:62): Cache Hit Ratio [com.atguigu.mybatis.mapper.CustomerMapper]: 0.75
8.数据查找过程:

二级缓存 -> 一级缓存 -> 数据库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息