由 Mybatis 源码畅谈软件设计(七):从根上理解 Mybatis 一级缓存
本篇我们来讲 一级缓存,重点关注它的实现原理:何时生效、生效范围和何时失效,在未来设计缓存使用时,提供一些借鉴和参考。
1. 准备工作
定义实体
public class Department {
public Department(String id) {
this.id = id;
}
private String id;
/**
* 部门名称
*/
private String name;
/**
* 部门电话
*/
private String tel;
/**
* 部门成员
*/
private Set<User> users;
}
public class User {
private String id;
private String name;
private Integer age;
private LocalDateTime birthday;
private Department department;
}
定义 Mapper.xml
DepartmentMapper.xml,两条 SQL:一条根据 ID 查询;一条清除缓存,标记了 fulshCache 标签,将其设置为 true 后,只要语句被调用,都会将本地缓存和二级缓存清空(默认值为 false)
<select id="findById" resultType="Department">
select * from department
where id = #{id}
</select>
<select id="cleanCathe" resultType="int" flushCache="true">
select count(department.id) from department;
</select>
UserMapper.xml,联表查询用户信息:
<select id="findAll" resultMap="userMap">
select u.*, td.id, td.name as department_name
from user u
left join department td
on u.department_id = td.id
</select>
2. 一级缓存
一级缓存的生效范围 SqlSession 级别的,不同 SqlSession 间不共享缓存,它默认情况下是启用的。主要作用是减少在同一个查询 SQL 会话中对数据库的重复查询,从而提高性能。以如下用例为例:
public static void main(String[] args) throws IOException {
InputStream xml = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 开启二级缓存需要在同一个SqlSessionFactory下,二级缓存存在于 SqlSessionFactory 生命周期,如此才能命中二级缓存
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(xml);
SqlSession sqlSession = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
}
可以发现在第二次查询时,一级缓存生效,控制台没有出现SQL:

而我们清空下一级缓存再试试:
public static void main(String[] args) throws IOException {
InputStream xml = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 开启二级缓存需要在同一个SqlSessionFactory下,二级缓存存在于 SqlSessionFactory 生命周期,如此才能命中二级缓存
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(xml);
SqlSession sqlSession = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------清除一级缓存 ↓------------");
departmentMapper.cleanCathe();
System.out.println("----------清除后department再一次查询,SQL再次出现 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
}
控制台日志很清晰,清除缓存后又重新查了一遍:

接下来我们看一下不同 SqlSession 间一级缓存是否共享,创建一个新的 SqlSession sqlSession1 执行相同的SQL:
public static void main(String[] args) throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession1 = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
DepartmentMapper departmentMapper1 = sqlSession1.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------sqlSession1下department执行相同的SQL,控制台出现SQL ↓------------");
departmentMapper1.findById("18ec781fbefd727923b0d35740b177ab");
}
如控制台日志所示,可以发现在不同的 SqlSession 下不共享一级缓存:

3. 一级缓存原理
一级缓存在查询方法 org.apache.ibatis.executor.BaseExecutor#query 中生效,如下所示:
public abstract class BaseExecutor implements Executor {
// ...
// 一级缓存
protected PerpetualCache localCache;
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
CacheKey key, BoundSql boundSql) throws SQLException {
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
// 判断是否刷新本地缓存
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List<E> list;
try {
queryStack++;
// 判断一级缓存是否存在,存在则直接作为结果返回,否则查询数据库
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
if (list != null) {
// 存储过程相关逻辑
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
// 未命中一级缓存,查询数据库
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
// issue #601
deferredLoads.clear();
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
// issue #482
clearLocalCache();
}
}
return list;
}
private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds,
ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
List<E> list;
// 一级缓存占位
localCache.putObject(key