概要
目的是提供一个思路,了解mybatis是缓存是如何工作的
版本号:
Mybatis 3.5.15
mysql-connector-java 8.0.33
缓存作用
第一次通过数据库查询之后,第二次进行相同的查询时就先从内存中去查询,有利于节省查询时间和减少数据库性能消耗
缓存开启状态
一级默认开启 属性名 cacheEnabled
二级缓存开启 Mapper文件中添加 <Cache />标签
缓存作用范围
一级缓存可以理解为对同一个SQLSession的请求生效,属于 SQLSession级别
二级缓存可以理解为对同一个Mapper下的namespace的请求生效,属于SQLSessionFactory级别
关键代码
一级缓存
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
关键是上面的这句:
从 PerpetualCache对象中 通过 CacheKey 获取值 这也是一级缓存的位置
第一次查询的时候会执行
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
这是一个私有方法,查询之后会通过localCache.putObject()方法放入键值对
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, EXECUTION_PLACEHOLDER);
try {
list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
} finally {
localCache.removeObject(key);
}
localCache.putObject(key, list);
if (ms.getStatementType() == StatementType.CALLABLE) {
localOutputParameterCache.putObject(key, parameter);
}
return list;
}
完整代码如下:类BaseExecutor
@Override
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;
}
二级缓存
通过MappedStatement获取Cache,然后通过TransactionalCacheManager (tcm)配合Cache 和CacheKey 获取真正的值
关键获取缓存的代码:
List<E> list = (List<E>) tcm.getObject(cache, key);
完整代码如下:类CachingExecutor
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler,
CacheKey key, BoundSql boundSql) throws SQLException {
Cache cache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
if (ms.isUseCache() && resultHandler == null) {
ensureNoOutParams(ms, boundSql);
@SuppressWarnings("unchecked")
List<E> list = (List<E>) tcm.getObject(cache, key);
if (list == null) {
list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
tcm.putObject(cache, key, list); // issue #578 and #116
}
return list;
}
}
return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
小结
通过代码可以知道最终获取缓存的途径就是使用PerpetualCache对象以及CacheKey来获取存储的缓存值,同时一级缓存的作用范围和二级缓存的作用范围是不同。