天天看點

第06篇:Mybatis緩存設計

​​官方文檔​​

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{
        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{
    // 從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檔案中添加

    标簽 (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,直到有對應的資料進入緩存。

一共可以使用的二級緩存有以下這些。

第06篇:Mybatis緩存設計

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>