前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis之二级缓存简析 原

Mybatis之二级缓存简析 原

作者头像
克虏伯
发布2019-04-15 11:11:56
2930
发布2019-04-15 11:11:56
举报

注:Mybatis的版本是3.5.0。

上一篇分析了一级缓存,这篇来分析二级缓存。

    以下的内容,跳过了很多细节,可以去看这篇博客

    一级缓存只能在同一个SqlSession中共享,而二级缓存则可以在多个SqlSession中共享。

    开启二级缓存,那么使用的是CachingExecutor和SimpleExecutor(不修改默认设置的情况下),如下List-1所示,SimpleExecutor是封装在CachingExecutor中的。

    List-1 

代码语言:javascript
复制
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);
  }
  if (cacheEnabled) {
    //将SimpleExecutor封装起来
    executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);
  return executor;
}

    CachingExecutor使用了委托模式,其属性"Executor delegate",

    List-2

代码语言:javascript
复制
package org.apache.ibatis.executor;

 ......
/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class CachingExecutor implements Executor {
    private final Executor delegate;
    private final TransactionalCacheManager tcm = new TransactionalCacheManager();

    public CachingExecutor(Executor delegate) {
        this.delegate = delegate;
        delegate.setExecutorWrapper(this);
    }
    ......

    如下图1所示,CachingExecutor中使用的Cache是SynchronizedCache,它里面还封装了很多Cache,最终数据是存储在PerpetualCache中,是个HashMap。

                            图1 CachingExecutor中使用的Cache是SynchronizedCache

    由于二级缓存Cache封装在SynchronizedCache中,所以对二级缓存的操作是线程安全的,SynchronizedCache的几乎每个方法上都加了Sychronized,这在实现线程安全的同时,也在一定程度上成了瓶颈,特别是对二级缓存操作的线程数量很多时。

    在List-2中的属性tcm,这个是理解二级缓存的一个关键点。

代码语言:javascript
复制
private final TransactionalCacheManager tcm = new TransactionalCacheManager();

    session1提交后,session2才能看到session1缓存的结果。

代码语言:txt
复制
 (adsbygoogle = window.adsbygoogle || []).push({});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/06/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档