美文网首页
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 缓存实现

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