开启二级缓存步骤
maven项目提前在pom.xml文件中增加相关依赖
- 在核心配置文件中加入
-
<settings> <setting name="cacheEnabled" value="true" /> </settings>
在映射配置文件中加入
回收策略:
LRU – 最近最少使用的:移除最长时间不被使用的对象。
FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
-
缓存遇到commit失效
-
ehcache二级缓存在映射配置文件中加入
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
添加maven依赖
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.9</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.2</version>
</dependency>
编写核心配置文件
<settings>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true" />
</settings>
编写映射配置文件
src/main/resources/com/lihaozhe/mapper/PersonMapper.xml
<!--
flushInterval(刷新间隔)属性可以被设置为任意的正整数,设置的值应该是一个以毫秒为单位的合理时间量。 默认情况是不设置,也就是没有刷新间隔,缓存仅仅会在调用语句时刷新。
size(引用数目)属性可以被设置为任意正整数,要注意欲缓存对象的大小和运行环境中可用的内存资源。默认值是 1024。
readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。 因此这些对象不能被修改。这就提供了可观的性能提 升。而可读写的缓存会(通过序列化)返回缓存对象的拷贝。 速度上会慢一些,但是更安全,因此默认值是 false。
-->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
在需要缓存的查找标签增加设置useCache=true
<select id="selectById" resultType="person" useCache="true">
<include refid="select_person"></include>
<where>
`id` = #{id}
</where>
</select>
编写测试类
package com.lihaozhe.mapper;
import com.lihaozhe.pojo.Person;
import com.lihaozhe.util.mybatis.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
/**
* @author 李昊哲
* @version 1.0.0 2022/7/12 上午10:05
*/
public class CacheTest {
@Test
public void testCache05() {
SqlSession sqlSession01 = MyBatisUtil.openSqlSession();
PersonMapper mapper01 = sqlSession01.getMapper(PersonMapper.class);
Person person01 = mapper01.selectById(3L);
MyBatisUtil.close();
SqlSession sqlSession02 = MyBatisUtil.openSqlSession();
PersonMapper mapper02 = sqlSession02.getMapper(PersonMapper.class);
Person person02 = mapper02.selectById(3L);
MyBatisUtil.close();
}
}
Cache Hit Ratio [com.lihaozhe.mapper.PersonMapper]: 0.0
Opening JDBC Connection
Created connection 1045731788.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
==> Preparing: SELECT `id`, `uuid`, `mobile`, `nickname`, `id_card` FROM `person` WHERE `id` = ?
==> Parameters: 3(Long)
<== Columns: id, uuid, mobile, nickname, id_card
<== Row: 3, 5ee0074d47064285b8c5925534d70d84, 17674856547, 令狐荡, 649152199504171242
<== Total: 1
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Returned connection 1045731788 to pool.
Cache Hit Ratio [com.lihaozhe.mapper.PersonMapper]: 0.5
package com.lihaozhe.mapper;
import com.lihaozhe.pojo.Person;
import com.lihaozhe.util.mybatis.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
/**
* @author 李昊哲
* @version 1.0.0 2022/7/12 上午10:05
*/
public class CacheTest {
@Test
public void testCache05() {
SqlSession sqlSession01 = MyBatisUtil.openSqlSession();
PersonMapper mapper01 = sqlSession01.getMapper(PersonMapper.class);
Person person01 = mapper01.selectById(3L);
MyBatisUtil.close();
SqlSession sqlSession02 = MyBatisUtil.openSqlSession();
PersonMapper mapper02 = sqlSession02.getMapper(PersonMapper.class);
Person person02 = mapper02.selectById(3L);
MyBatisUtil.close();
}
}
Cache Hit Ratio [com.lihaozhe.mapper.PersonMapper]: 0.0
Opening JDBC Connection
Created connection 1045731788.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
==> Preparing: SELECT `id`, `uuid`, `mobile`, `nickname`, `id_card` FROM `person` WHERE `id` = ?
==> Parameters: 6(Long)
<== Columns: id, uuid, mobile, nickname, id_card
<== Row: 6, aebe1bd701b24fb89347c78bb3aaf938, 13651227685, 欧阳脓, 644102199803081660
<== Total: 1
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Returned connection 1045731788 to pool.
Cache Hit Ratio [com.lihaozhe.mapper.PersonMapper]: 0.0
Opening JDBC Connection
Checked out connection 1045731788 from pool.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
==> Preparing: SELECT `id`, `uuid`, `mobile`, `nickname`, `id_card` FROM `person` WHERE `id` = ?
==> Parameters: 3(Long)
<== Columns: id, uuid, mobile, nickname, id_card
<== Row: 3, 5ee0074d47064285b8c5925534d70d84, 17674856547, 令狐荡, 649152199504171242
<== Total: 1
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3e5499cc]
Returned connection 1045731788 to pool.