官方文档
MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制。本篇文章,小编将会在最短的时间呢,通过观察源码来深刻了解Mybatis的一、Mybatis Cache设计 在Mybatis中所有的缓存,都是实现自Cache接口。无论是一级缓存还是二级缓存都是实现这个接口。其中一级缓存是本地缓存,二级缓存是一个允许开发者扩展的
一级二级缓存; 然后在说如何定制。
缓存(eg: ehcache/或者内置的很多缓存)。
public interface Cache {String getId();
void putObject(Object key, Object value);
Object getObject(Object key);
Object removeObject(Object key);
void clear();
int getSize();
default ReadWriteLock getReadWriteLock() {
return null;
}}
二、一级缓存 一级缓存是本地缓存,其实就是PerpetualCache这类,它的源码也很简单,其实就是一个Map而已。一般面试的经常说一级缓存称为
SqlSession缓存,我们看其实最终实现是在BaseExecutor进行做的。就这么简单。
public abstract class BaseExecutor implements Executor {// 一级缓存本地缓存
protected PerpetualCache localCache;
protected BaseExecutor(Configuration configuration, Transaction transaction) {
this.transaction = transaction;
this.deferredLoads = new ConcurrentLinkedQueue<>();
this.localCache = new PerpetualCache("LocalCache");
}// 执行查询后添加到一级缓存中
private List queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
List 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;
}
}
三、二级缓存 二级缓存是基于装饰器模式,它允许开发者自定义缓存的实现,只要实现了Cache接口就行。通过装饰器的设计。
CachingExecutor从MappedStatement#getCache获取缓存的具体实现,从而进行缓存操作。
下面代码是看Mybatis是如何进行装饰器的。注意看注释。如果开启缓存,则包装器对Executor进行包装。
public class Configuration {
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
// 如果开启缓存,则包装器对Executor进行包装
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
}
CachingExecutor在实际执行时候从MappedStatement#getCache获取缓存的具体实现,从而进行缓存操作。
看到查询是先从二级缓存中获取,如果没有获取到就从一级缓存中获取,还没有就查询db。
public class CachingExecutor implements Executor {@Override
public List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
throws SQLException {
// 从MappedStatement获取Cache
Cache cache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
if (ms.isUseCache() && resultHandler == null) {
ensureNoOutParams(ms, boundSql);
@SuppressWarnings("unchecked")
List list = (List) 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);
}
}
注意这里可以看到如果指定了要进行缓存,但是没有指定缓存的type默认是 PERPETUAL(PerpetualCache
)
四、开启二级缓存 4.1 内置二级缓存
- 首先开启配置
- 同时在Mapper文件中添加
标签 (XMLMapperBuilder#cacheElement) - 或者是在Mapper类上添加@CacheNamespace注解(MapperAnnotationBuilder#parseCache)
// 通过 cacheEnabled 进行配置,如果不配置默认是true
// 添加cache标签
属性 | 含义 |
---|---|
eviction | 缓存回收策略 |
flushInterval | 缓存刷新间隔,缓存多长时间刷新一次,默认不清空,设置一个毫秒值 |
readOnly | 是否只读;true 只读 |
size | 缓存存放多少个元素 |
type | 指定自定义缓存的全类名(实现Cache 接口即可) |
blocking | 若缓存中找不到对应的key,是否会一直blocking,直到有对应的数据进入缓存。 |
文章图片
4.2 外置二级缓存 只要实现了Cache接口那么Mybatis就会调用这个接口实现进行缓存。下面只说一个思路。如下通过指定EhcacheCache
就可以将这个二级缓存的能力,交给Mybatis进行调用了。
感谢您的阅读,本文由 西魏陶渊明 版权所有。如若转载,请注明出处:西魏陶渊明(https://blog.springlearn.cn/)
【第06篇:Mybatis缓存设计】本文由mdnice多平台发布
推荐阅读
- 低代码渲染那些事
- [ 每周译Go ] 《How to Code in Go》系列第二篇
- 测试用例|实操自动生成接口自动化测试用例
- python|Python基于机器学习方法实现的电影推荐系统
- c|C/C++学习资源(百度云盘链接)
- 禅智听书《精进(如何成为一个很厉害的人》)
- 不算不知道,花呗分期的真实利率居然这么高
- 程序员|【python教程入门学习】普通人学python有意义吗
- 企业微信SCRM系统帮助企业提高团队效率