在测试类中调用 mapper的selectOne() 方法,缓存失效,还是执行了10次查询
@Test
public void test_mybatis_plus_cache(){
// 循环执行 selectOne()
for (int i = 0; i < 10; i++) {
sequenceMapper.selectOne(Wrappers.<Sequence>lambdaQuery().eq(Sequence::getType,1));
}
}
断点调试时 每次的 SqlSession 对象都是同一个 并且每次生成的 CacheKey 对象都是相同的
第一次
- 这是第一次获取到的 SqlSession 对象
- 这是第一次生成的cacheKey
第二次
- 这是第二次获取到的 SqlSession 对象
- 这是第二次生成的cacheKey
根据网上查询到的资料来看 sqlSession 和 Cachekey 一样应该第二次走缓存的 但是并没有
失效的位置
这里内部清除了 localCache中的缓存
// 这里是最终调用清除缓存的代码
@Override
public void close(boolean forceRollback) {
try {
try {
rollback(forceRollback);
} finally {
if (transaction != null) {
transaction.close();
}
}
} catch (SQLException e) {
// Ignore. There's nothing that can be done at this point.
log.warn("Unexpected exception on closing transaction. Cause: " + e);
} finally {
// finally块中清除了 localCache 中的缓存
transaction = null;
deferredLoads = null;
localCache = null;
localOutputParameterCache = null;
closed = true;
}
}