美文网首页
mybatis 缓存实现

mybatis 缓存实现

作者: 全都是泡沫啦 | 来源:发表于2018-12-27 18:10 被阅读0次

mybatis查询缓存分为一级缓存(相同会话SqlSession中Executor的PerpetualCache=localCache)和二级缓存(相同MappedStatement的id共用缓存)

  1. 一级缓存的创建:BaseExecutor.PerpetualCache (PerpetualCache内部为HashMap)
  protected PerpetualCache localCache = new PerpetualCache("LocalCache");
  1. 二级缓存的使用:解析@CacheNamespace或者使用<cache />
    注解org.apache.ibatis.annotations.CacheNamespace 使用org.apache.ibatis.builder.annotation.MapperAnnotationBuilder解析
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CacheNamespace {
  Class<? extends org.apache.ibatis.cache.Cache> implementation() default PerpetualCache.class;

  Class<? extends org.apache.ibatis.cache.Cache> eviction() default LruCache.class;

  long flushInterval() default 0;

  int size() default 1024;

  boolean readWrite() default true;
  
  boolean blocking() default false;
  
}


private void parseCache() {
    CacheNamespace cacheDomain = type.getAnnotation(CacheNamespace.class);
    if (cacheDomain != null) {
      Integer size = cacheDomain.size() == 0 ? null : cacheDomain.size();
      Long flushInterval = cacheDomain.flushInterval() == 0 ? null : cacheDomain.flushInterval();
      assistant.useNewCache(cacheDomain.implementation(), cacheDomain.eviction(), flushInterval, size, cacheDomain.readWrite(), cacheDomain.blocking(), null);
    }
  }

xml解析:org.apache.ibatis.builder.xml.XMLMapperBuilder

private void cacheElement(XNode context) throws Exception {
    if (context != null) {
      String type = context.getStringAttribute("type", "PERPETUAL");
      Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
      String eviction = context.getStringAttribute("eviction", "LRU");
      Class<? extends Cache> evictionClass = typeAliasRegistry.resolveAlias(eviction);
      Long flushInterval = context.getLongAttribute("flushInterval");
      Integer size = context.getIntAttribute("size");
      boolean readWrite = !context.getBooleanAttribute("readOnly", false);
      boolean blocking = context.getBooleanAttribute("blocking", false);
      Properties props = context.getChildrenAsProperties();
      builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
    }
  }
  1. 二级缓存的创建及包装:build通过参数flushInterval size readWrite blocking进行包装
public Cache useNewCache(Class<? extends Cache> typeClass,
      Class<? extends Cache> evictionClass,
      Long flushInterval,
      Integer size,
      boolean readWrite,
      boolean blocking,
      Properties props) {
    Cache cache = new CacheBuilder(currentNamespace)
        .implementation(valueOrDefault(typeClass, PerpetualCache.class))
        .addDecorator(valueOrDefault(evictionClass, LruCache.class))
        .clearInterval(flushInterval)
        .size(size)
        .readWrite(readWrite)
        .blocking(blocking)
        .properties(props)
        .build();
    configuration.addCache(cache);
    currentCache = cache;
    return cache;
  }
  1. 内置的cache实现类和包装类(cache实现细节自己慢慢品味吧尤其是LruCache SoftCache WeakCache)
Cache
PerpetualCache
BlockingCache
FifoCache
LoggingCache
LruCache
ScheduledCache
SerializedCache
SoftCache
SynchronizedCache
TransactionalCache
WeakCache

相关文章

  • Mybatis的缓存

    一 Mybatis缓存体系图 Mybatis缓存的基础实现是perpetualCache,但是mybatis利用装...

  • MyBatis组件之缓存实现及使用

    一 .概述 先讲缓存实现,主要是mybatis一级缓存,二级缓存及缓存使用后续补充 Mybatis缓存的实现是基于...

  • 聊聊缓存

    本地缓存:mybatis实现:装饰器模式实践 PerpetualCache:永久缓存:通过HashMap实现最大容...

  • java基础面试题总结——其他大型框架

    1. 简述mybatis缓存机制的实现原理 mybatis缓存分为一级缓存和二级缓存: 一级缓存 概念:一级缓存即...

  • MyBatis-Mapper动态代理

    MyBatis官方教程MyBatis二级缓存设计Mybatis中Mapper动态代理的实现原理制作Mybatis插...

  • mybatis 缓存实现

    mybatis查询缓存分为一级缓存(相同会话SqlSession中Executor的PerpetualCache=...

  • MyBatis缓存和注解

    Mybatis缓存和注解 学习目标 1、mybatis缓存 2、mybatis注解 学习内容 1、mybatis缓...

  • mybatis MyBatis缓存实现类

    起因:在工作中常常要用到mybatis框架,如果对其执行流程不清楚的话,就会有一种出了bug不知道要去什么地方找的...

  • 七. Mybatis二级缓存

    概述 mybatis二级缓存是基于redis实现的,必须先配置好redis 一. 缓存类的实现 要想使用Myba...

  • MyBatis缓存书目录

    MyBatis缓存 MyBatis介绍 MyBatis一级缓存 1、什么是一级缓存? 为什么使用一级缓存? 2、M...

网友评论

      本文标题:mybatis 缓存实现

      本文链接:https://www.haomeiwen.com/subject/wyqdlqtx.html