zoukankan      html  css  js  c++  java
  • Mybatis-09

    Mybatis

    Mybatis缓存

      Mybatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。在Mybatis系统中默认定义了两个缓存:一级缓存、二级缓存。

    一级缓存

      一级缓存默认情况下是开启的,是SqlSession级别的缓存,即本地缓存。

    二级缓存

      二级缓存是基于namespace级别的缓存,即全局缓存,需要手动开启和配置。我们可以通过Cache接口来自定义二级缓存。

      由于当会话关闭或提交时,一级缓存将会消失,假设我们需要这个数据依旧保存,则我们需要用到二级缓存。

    开启缓存的步骤:

      首先在配置文件中:

    <!-- 开启全局缓存 -->
    <setting name="cacheEnable" value="true"/>

      接着在相应的mapper层下开启,当然也可以直接<cache/>,里面的参数都可以自定义。

    <cache
      eviction="FIFO"
      flushInterval="60000"
      size="512"
      readOnly="true"/>

    自定义缓存:Ehcache

      EhCache 是一个纯java进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。

      要使用它,我们首先需要导入相应的maven依赖

    <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
    <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.0.3</version>
    </dependency>

      接着编写它的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    
    <diskStore path="./tmpdir/Tmp_EhCache"/>
    
    <defaultCache
    eternal="false"
    maxElementsInMemory="10000"
    overflowToDisk="false"
    diskPersistent="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="259200"
    memoryStoreEvictionPolicy="LRU"/>
    
    <cache
    name="cloud_user"
    eternal="false"
    maxElementsInMemory="5000"
    overflowToDisk="false"
    diskPersistent="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="1800"
    memoryStoreEvictionPolicy="LRU"/>
    </ehcache>

      最后在相应的mapper层开启

    <cache type="org.mybatis.cache.ehcache"/>
  • 相关阅读:
    json 轻解读 转
    android file.mkdir()
    iOS摄像头采集和编码
    对安装React脚手架出错的情况做以详解
    DNGuard Enterprise v2.95 released
    DNGuard Enterprise v3.2 released
    DNGuard 专业版 v2.95 发布
    DNGuard 企业版 v3.1 发布
    Windows 2003 上使用 Windows Live Writer
    .Net 中枚举AppDomains
  • 原文地址:https://www.cnblogs.com/Charles-H/p/Mybatis-09.html
Copyright © 2011-2022 走看看