mysql一级缓存和二级缓存_Mybatis 源码分析(三)之 Mybatis 的一级缓存和二级缓存...

5515640d14fe

2.jpg

Mybatis 源码分析(三)之 Mybatis 的一级缓存和二级缓存

Mybatis缓存的作用

每当我们使用 MyBatis 开启一次和数据库的会话,MyBatis 会创建出一个 SqlSession 对象表示一次数据库会话。

在对数据库的一次会话中,我们有可能会反复地执行完全相同的查询语句,如果不采取一些措施的话,每一次查询都会查询一次数据库,而我们在极短的时间内做了完全相同的查询,那么它们的结果极有可能完全相同,由于查询一次数据库的代价很大,这有可能造成很大的资源浪费。

为了解决这一问题,减少资源的浪费,MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候,如果判断先前有个完全一样的查询,会直接从缓存中直接将结果取出,返回给用户,不需要再进行一次数据库查询了。

mybatis的缓存有一级缓存和二级缓存。

一级缓存

一级缓存是默认开启的,作用域是session级别的,缓存的key格式如下:

cache key: id + sql + limit + offset

在commit之前,第一次查询结果换以key value的形式存起来,如果有相同的key进来,直接返回value,这样有助于减轻数据的压力。

相关源码:

org.apache.ibatis.executor.BaseExecutor#createCacheKey

public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {

if (this.closed) {

throw new ExecutorException("Executor was closed.");

} else {

CacheKey cacheKey = new CacheKey();

cacheKey.update(ms.getId());

cacheKey.update(rowBounds.getOffset());

cacheKey.update(rowBounds.getLimit());

cacheKey.update(boundSql.getSql());

List parameterMappings = boundSql.getParameterMappings();

TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();

for(int i = 0; i < parameterMappings.size(); ++i) {

ParameterMapping parameterMapping = (ParameterMapping)parameterMappings.get(i);

if (parameterMapping.getMode() != ParameterMode.OUT) {

String propertyName = parameterMapping.getProperty();

Object value;

if (boundSql.hasAdditionalParameter(propertyName)) {

value = boundSql.getAdditionalParameter(propertyName);

} else if (parameterObject == null) {

value = null;

} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {

value = parameterObject;

} else {

MetaObject metaObject = this.configuration.newMetaObject(parameterObject);

value = metaObject.getValue(propertyName);

}

cacheKey.update(value);

}

}

if (this.configuration.getEnvironment() != null) {

cacheKey.update(this.configuration.getEnvironment().getId());

}

return cacheKey;

}

}

查询数据库并存入一级缓存的语句

org.apache.ibatis.executor.BaseExecutor#queryFromDatabase

private List queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {

this.localCache.putObject(key, ExecutionPlaceholder.EXECUTION_PLACEHOLDER);

List list;

try {

list = this.doQuery(ms, parameter, rowBounds, resultHandler, boundSql);

} finally {

this.localCache.removeObject(key);

}

//将查询出来的结果存入一级缓存

this.localCache.putObject(key, list);

if (ms.getStatementType() == StatementType.CALLABLE) {

//如果是存储过程把参数存入localOutputParameterCache

this.localOutputParameterCache.putObject(key, parameter);

}

return list;

}

并且当commit或者rollback的时候会清除缓存,并且当执行insert、update、delete的时候也会清除缓存。

相关源码:

org.apache.ibatis.executor.BaseExecutor#update

public int update(MappedStatement ms, Object parameter) throws SQLException {

ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());

if (this.closed) {

throw new ExecutorException("Executor was closed.");

} else {

//删除一级缓存

this.clearLocalCache();

return this.doUpdate(ms, parameter);

}

}

org.apache.ibatis.executor.BaseExecutor#commit

public void commit(boolean required) throws SQLException {

if (this.closed) {

throw new ExecutorException("Cannot commit, transaction is already closed");

} else {

//删除一级缓存

this.clearLocalCache();

this.flushStatements();

if (required) {

this.transaction.commit();

}

}

}

org.apache.ibatis.executor.BaseExecutor#rollback

public void rollback(boolean required) throws SQLException {

if (!this.closed) {

try {

//删除一级缓存

this.clearLocalCache();

this.flushStatements(true);

} finally {

if (required) {

this.transaction.rollback();

}

}

}

}

二级缓存

二级缓存是手动开启的,作用域为sessionfactory(也可以说MapperStatement级缓存,也就是一个namespace就会有一个缓存),因为二级缓存的数据不一定都是存储到内存中,它的存储介质多种多样,实现二级缓存的时候,MyBatis要求返回的POJO必须是可序列化的,也就是要求实现Serializable接口,如果存储在内存中的话,实测不序列化也可以的。

如果开启了二级缓存的话,你的Executor将会被装饰成CachingExecutor,缓存是通过CachingExecutor来操作的,查询出来的结果会存在statement中的cache中,若有更新,删除类的操作默认就会清空该MapperStatement的cache(也可以通过修改xml中的属性,让它不执行),不会影响其他的MapperStatement。

相关源码:

org.apache.ibatis.session.Configuration#newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {

executorType = executorType == null ? this.defaultExecutorType : executorType;

executorType = executorType == null ? ExecutorType.SIMPLE : executorType;

Object 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);

}

//是否开启缓存,传入的参数为SimpleExecutor

if (this.cacheEnabled) {

executor = new CachingExecutor((Executor)executor);

}

//责任链模式拦截器

Executor executor = (Executor)this.interceptorChain.pluginAll(executor);

return executor;

}

query

org.apache.ibatis.executor.CachingExecutor#query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)

public List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {

BoundSql boundSql = ms.getBoundSql(parameterObject);

CacheKey key = this.createCacheKey(ms, parameterObject, rowBounds, boundSql);

return this.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

}

org.apache.ibatis.executor.CachingExecutor#query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)

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) {

//看是否需要清除cache(在xml中可以配置flushCache属性决定何时清空cache)

this.flushCacheIfRequired(ms);

//若开启了cache且resultHandler 为空

if (ms.isUseCache() && resultHandler == null) {

this.ensureNoOutParams(ms, parameterObject, boundSql);

//从TransactionalCacheManager中取cache

List list = (List)this.tcm.getObject(cache, key);

//若取出来list是空的

if (list == null) {

//查询数据库

list = this.delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

//将结果存入cache中

this.tcm.putObject(cache, key, list);

}

return list;

}

}

//如果缓存为空,去查询数据库

return this.delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

}

对于

this.tcm.getObject(cache, key);

因同一个namespace下的MappedStatement的cache是同一个,而TransactionalCacheManager中统一管理cache是里面的属性transactionalCaches,该属性以MappedStatement中的Cache为key,TransactionalCache对象为Value。即一个namespace对应一个TransactionalCache。

相关源码:

TransactionalCacheManager

org.apache.ibatis.cache.TransactionalCacheManager

public class TransactionalCacheManager {

private Map transactionalCaches = new HashMap();

...

}

TransactionalCache

org.apache.ibatis.cache.decorators.TransactionalCache

public class TransactionalCache implements Cache {

private static final Log log = LogFactory.getLog(TransactionalCache.class);

//namespace中的cache

private Cache delegate;

//提交的时候清除cache的标志位

private boolean clearOnCommit;

//待提交的集合

private Map entriesToAddOnCommit;

//未查到的key存放的集合

private Set entriesMissedInCache;

...

}

update

//更新

org.apache.ibatis.executor.CachingExecutor#update

public int update(MappedStatement ms, Object parameterObject) throws SQLException {

//看是否需要清除cache(在xml中可以配置flushCache属性决定何时清空cache)

this.flushCacheIfRequired(ms);

return this.delegate.update(ms, parameterObject);

}

org.apache.ibatis.executor.CachingExecutor#flushCacheIfRequired

private void flushCacheIfRequired(MappedStatement ms) {

//获得cache

Cache cache = ms.getCache();

//若isFlushCacheRequired为true,则清除cache

if (cache != null && ms.isFlushCacheRequired()) {

this.tcm.clear(cache);

}

}

一级、二级缓存测试

因为一级缓存是默认生效的,下面是二级缓存开启步骤。

mybatis-config.xml

在mapper.xml可以进行如下的配置

update user

set name = #{name,jdbcType=VARCHAR},

age = #{age,jdbcType=INTEGER}

where id = #{id,jdbcType=INTEGER}

select

from user

where id = #{id,jdbcType=INTEGER}

其中仅仅添加下面这个也可以

如果我们配置了二级缓存就意味着:

映射语句文件中的所有select语句将会被缓存。

映射语句文件中的所欲insert、update和delete语句会刷新缓存。

缓存会使用默认的Least Recently Used(LRU,最近最少使用的)算法来收回。

根据时间表,比如No Flush Interval,(CNFI没有刷新间隔),缓存不会以任何时间顺序来刷新。

缓存会存储列表集合或对象(无论查询方法返回什么)的1024个引用。

缓存会被视为是read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全的被调用者修改,不干扰其他调用者或线程所做的潜在修改。

User.java

public class User implements Serializable {

private Integer id;

private String name;

private Integer age;

private static final long serialVersionUID = 1L;

...

set/get

...

}

UserMapper.java

User selectByPrimaryKey(Integer id);

测试方法

@Test

public void test03() throws IOException {

SqlSession sqlSession = sqlSessionFactory.openSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

//第一次

User user = userMapper.selectByPrimaryKey(1);

System.out.println("user1 => " + user.toString());

//第二次

User user2 = userMapper.selectByPrimaryKey(1);

System.out.println("user2 => " + user2.toString());

//session提交

sqlSession.commit();

SqlSession sqlSession2 = sqlSessionFactory.openSession();

UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);

//第三次

User user3 = userMapper2.selectByPrimaryKey(1);

System.out.println("user3 => " + user3.toString());

//第四次

User user4 = userMapper2.selectByPrimaryKey(1);

System.out.println("user4 => " + user4.toString());

sqlSession2.commit();

}

来看下结果

DEBUG 2019-01-30 00:01:29791 Opening JDBC Connection

DEBUG 2019-01-30 00:01:34688 Created connection 1121453612.

DEBUG 2019-01-30 00:01:34689 Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@42d8062c]

DEBUG 2019-01-30 00:01:34691 ==> Preparing: select id, name, age from user where id = ?

DEBUG 2019-01-30 00:01:34737 ==> Parameters: 1(Integer)

DEBUG 2019-01-30 00:01:34757 <== Total: 1

user1 => User{id=1, name='ayang', age=18}

DEBUG 2019-01-30 00:01:34757 Cache Hit Ratio [com.demo.mybatis.mapper.UserMapper]: 0.0

user2 => User{id=1, name='ayang', age=18}

DEBUG 2019-01-30 00:01:34818 Cache Hit Ratio [com.demo.mybatis.mapper.UserMapper]: 0.3333333333333333

user3 => User{id=1, name='ayang', age=18}

DEBUG 2019-01-30 00:01:34819 Cache Hit Ratio [com.demo.mybatis.mapper.UserMapper]: 0.5

user4 => User{id=1, name='ayang', age=18}

可以看到第一次和第二次走的是一级缓存,第三次和第四次走的是二级缓存。

总结:

一级缓存是自动开启的,sqlSession级别的缓存,查询结果存放在BaseExecutor中的localCache中。

如果第一次做完查询,接着做一次update | insert | delete | commit | rollback操作,则会清除缓存,第二次查询则继续走数据库。

对于一级缓存不同的sqlSession之间的缓存是互相不影响的。

二级缓存是手动开启的,作用域为sessionfactory,也可以说MapperStatement级缓存,也就是一个namespace(mapper.xml)就会有一个缓存,不同的sqlSession之间的缓存是共享的。

因为二级缓存的数据不一定都是存储到内存中,它的存储介质多种多样,实现二级缓存的时候,MyBatis要求返回的POJO必须是可序列化的,也就是要求实现Serializable接口,如果存储在内存中的话,实测不序列化也可以的。

一般为了避免出现脏数据,所以我们可以在每一次的insert | update | delete操作后都进行缓存刷新,也就是在Statement配置中配置flushCache属性,如下:

update user

set name = #{name,jdbcType=VARCHAR},

age = #{age,jdbcType=INTEGER}

where id = #{id,jdbcType=INTEGER}

如有问题欢迎留言:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL并没有官方支持的一级缓存二级缓存。引用中提到的一级缓存和引用中提到的二级缓存MyBatis框架中的缓存机制。MySQL自身并没有这样的缓存机制。在MySQL中,通常会使用MySQL自身的查询缓存来优化查询性能。 MySQL的查询缓存是一种基于SQL语句的缓存机制,它会将查询结果缓存在内存中以供后续查询使用。当执行一个查询语句时,MySQL会首先检查查询缓存是否有该查询的缓存结果,如果有,则直接返回缓存结果,而不需要再次执行查询操作。这可以大大提高查询性能。然而,MySQL的查询缓存机制存在一些缺陷,比如对于更新操作的表,如果执行了更新操作,那么相应的查询缓存将被清除,从而导致下一次查询无法命中缓存。 除了MySQL自身的查询缓存,还可以通过其他方式来实现缓存。比如,使用外部的缓存系统(如Redis、Memcached等)来缓存查询结果,或者在应用程序层面通过代码实现缓存。这些缓存方式可以提供更灵活、更可控的缓存机制,但也需要开发人员自行实现和管理。 总结来说,MySQL本身并没有一级缓存二级缓存的概念,但可以通过MySQL的查询缓存或其他缓存机制来提高查询性能和数据访问效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mysql一级缓存二级缓存](https://blog.csdn.net/qiuhui123456/article/details/98509360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MYSQL一级缓存二级缓存](https://blog.csdn.net/qq_51250453/article/details/119582234)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值