@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 一样应该第二次走缓存的 但是并没有
这里内部清除了 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;
}
}
在spring对一级缓存的设计就是这样的,当然在关闭session里面可以看到有个从事务管理器里获取session的操作,也就是当前查询如果开启事务那么会使用到一级缓存。
有没有可能是你的缓存没配置好