关于程序员:第06篇Mybatis缓存设计

2次阅读

共计 4405 个字符,预计需要花费 12 分钟才能阅读完成。

官网文档

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 <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;
      }
}  

三、二级缓存

二级缓存是基于装璜器模式, 它容许开发者自定义缓存的实现, 只有实现了 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 <E> List<E> 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<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);
  }
}

留神这里能够看到如果指定了要进行缓存,然而没有指定缓存的 type 默认是 PERPETUAL(PerpetualCache
)

四、开启二级缓存

4.1 内置二级缓存

  1. 首先开启配置
  2. 同时在 Mapper 文件中增加 <cache/> 标签 (XMLMapperBuilder#cacheElement)
  3. 或者是在 Mapper 类上增加 @CacheNamespace 注解(MapperAnnotationBuilder#parseCache)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 指定 Mybatis 应用 log4j -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
        // 通过 cacheEnabled 进行配置, 如果不配置默认是 true
        <setting name="cacheEnabled" value="false"/>
    </settings>
</configuration>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="orm.example.dal.mapper.TUserMapper">
    // 增加 cache 标签
    <cache/>
</mapper>    
属性 含意
eviction 缓存回收策略
flushInterval 缓存刷新距离,缓存多长时间刷新一次,默认不清空,设置一个毫秒值
readOnly 是否只读;true 只读
size 缓存寄存多少个元素
type 指定自定义缓存的全类名(实现 Cache 接口即可)
blocking 若缓存中找不到对应的 key,是否会始终 blocking,直到有对应的数据进入缓存。

一共能够应用的二级缓存有以下这些。

4.2 外置二级缓存

只有实现了 Cache 接口那么 Mybatis 就会调用这个接口实现进行缓存。上面只说一个思路。如下通过指定 EhcacheCache
就能够将这个二级缓存的能力,交给 Mybatis 进行调用了。

<cache type="org.mybatis.caches.ehcache.EhcacheCache" > 
      <property name="timeToIdleSeconds" value="3600"/>
      <property name="timeToLiveSeconds" value="3600"/>
      <!-- 同 ehcache 参数 maxElementsInMemory -->
    <property name="maxEntriesLocalHeap" value="1000"/>
    <!-- 同 ehcache 参数 maxElementsOnDisk -->
      <property name="maxEntriesLocalDisk" value="10000000"/>
      <property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>

感谢您的浏览,本文由 西魏陶渊明 版权所有。如若转载,请注明出处:西魏陶渊明(https://blog.springlearn.cn/)

本文由 mdnice 多平台公布

正文完
 0