本文主要是介绍mybatis源码 (四) —— 一级缓存和二级缓存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MyBatis的缓存分为两种
- 一级缓存,一级缓存是SqlSession级别的缓存,对于相同的查询,会从缓存中返回结果而不是查询数据库(也叫本地缓存)默认会启用
- 二级缓存,二级缓存是Mapper级别的缓存,定义在Mapper文件的标签中并需要开启此缓存,默认关闭
先看二级缓存:开启
org.apache.ibatis.executor.CachingExecutor#query
//被ResultLoader.selectList调用@Overridepublic <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)throws SQLException {Cache cache = ms.getCache();//默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/>//简单的说,就是先查CacheKey,查不到再委托给实际的执行器去查if (cache != null) {flushCacheIfRequired(ms);if (ms.isUseCache() && resultHandler == null) {ensureNoOutParams(ms, parameterObject, boundSql);@SuppressWarnings("unchecked")List<E> list = (List<E>) tcm.getObject(cache, key);if (list == null) {list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);tcm.putObject(cache, key, list); // issue #578 and #116}return list;}}return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);}
- 根据flushCache=true或者flushCache=false判断是否要清理二级缓存
- 从tcm中获取查询结果,这个tcm解释一下,
- 如果没有从MyBatis二级缓存中拿到数据,那么就会查一次数据库,然后放到MyBatis二级缓存中去
总结:二级缓存是存在MappedStatement 即mapper中
再看一级缓存:
org.apache.ibatis.executor.BaseExecutor#query
@Overridepublic <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.");}//先清局部缓存,再查询.但仅查询堆栈为0,才清。为了处理递归调用if (queryStack == 0 && ms.isFlushCacheRequired()) {clearLocalCache();}List<E> list;try {//加一,这样递归调用到上面的时候就不会再清局部缓存了queryStack++;//先根据cachekey从localCache去查list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;if (list != null) {//若查到localCache缓存,处理localOutputParameterCachehandleLocallyCachedOutputParameters(ms, key, parameter, boundSql);} else {//从数据库查list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);}
- 先根据cachekey从localCache去查
- 若查到localCache缓存,处理localOutputParameterCache
- 没查到再从数据库查
//从数据库查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);//如果是存储过程,OUT参数也加入缓存if (ms.getStatementType() == StatementType.CALLABLE) {localOutputParameterCache.putObject(key, parameter);}return list;}
从数据库查到数据之后保存在一级缓存里面
总结:一级缓存是在BaseExecutor中维护的,也就是说一级缓存对应的SQLSession
而对于org.apache.ibatis.executor.CachingExecutor#update
@Overridepublic int update(MappedStatement ms, Object parameterObject) throws SQLException {//刷新缓存完再updateflushCacheIfRequired(ms);return delegate.update(ms, parameterObject);}
二级缓存的更新操作,先清除缓存,在更新
org.apache.ibatis.executor.BaseExecutor#update
@Overridepublic int update(MappedStatement ms, Object parameter) throws SQLException {ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());if (closed) {throw new ExecutorException("Executor was closed.");}//先清局部缓存,再更新,如何更新交由子类,模板方法模式clearLocalCache();return doUpdate(ms, parameter);}
一级缓存的更新操作,先清除缓存,在更新
这篇关于mybatis源码 (四) —— 一级缓存和二级缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!