MyBatis--查询缓存

MyBatis–查询缓存

查询缓存的使用,主要是为了提高查询访问速度。将用户对同一数据的重复查询过程简化,不再每次均从数据库查询获取结果数据,从而提高访问速度。

MyBatis的查询缓存机制,根据缓存区的作用域(生命周期)可划分为两种:一级缓存与二级缓存

一、一级查询缓存

MyBatis一级缓存是基于org.apache.ibatis.cache.impl.PerpetualCache类的HashMap本地缓存,其作用域是Sqlsession。在同一个Sqlsession中两次执行相同的sql语句,第一次执行完毕后,会将查询结果写入到缓存中,第二次会从缓存中直接获取数据,而不再到数据库中进行查询,从而提高查询效率。

当一个Sqlsession结束后,该Sqlsession中的一级缓存也就不存在了。MyBatis默认一级缓存是开启状态,且不能关闭。

执行原理示意.PNG

1.一级缓存的存在性证明

测试类:

//证明一级缓存的存在
@Test
public void test01(){
    //第一次查询
    Student student = dao.selectStudentById(2);
    System.out.println(student);
    //第二次查询
    Student student2 = dao.selectStudentById(2);
    System.out.println(student2);       
}

mapper:

<mapper namespace="com.hcx.dao.IStudentDao">     
    <select id=selectStudentById resultType="com.hcx.beans.Student">
        select * from student where id=#{id}
    </select>
</mapper>

控制台:

执行完后,发现只执行了一次从DB中的查询,第二次的结果是直接输出的。说明,第二次是从Sqlsession缓存中读取的。

2.PNG

2.从缓存读取数据的依据是sql的id

一级缓存缓存的是相同sql映射id的查询结果,而非相同sql语句的查询结果。因为MyBatis内部对于查询缓存,无论是一级查询还是二级查询,其底层均使用一个hashmap实现:key为sql的id相关内容,value为从数据库中查询出的结果。

mapper:

<mapper namespace="com.hcx.dao.IStudentDao">

    <select id=selectStudentById resultType="com.hcx.beans.Student">
        select * from student where id=#{id}
    </select>

    <select id="selectStudnetById2" resultType="com.hcx.beans.Student">
        select id,name,age,score,birthday from student where id=#{id}
    </select>
</mapper>

dao接口:

public interface IStudentDao {      
    Student selectStudentById(int id);
    Student selectStudentById2(int id); 
}

测试类:

//证明从一级缓存中读取数据的依据:
//MyBatis:sql的id+sql语句
//hibernate:查询结果对象的id
@Test
public void test02(){
    Student student = dao.selectStudentById(2);
    System.out.println(student);

    Student student2 = dao.selectStudentById2(2);
    System.out.println(student2);       
}

控制台:

查看控制台,发现第二次查询结果与第一次的完全相同,但第二次查询并没有从缓存中读取数据,而是直接从DB中进行的查询。这是因为从缓存读取数据的依据是查询sql的映射id,而非查询结果。

3.PNG

3.增删改对一级查询缓存的影响

增删改操作,无论是否进行提交Sqlsession.commit(),均会清空一级查询缓存,使查询再次从DB中select。

测试类:

@Test
public void test03(){
    Student student = dao.selectStudentById(2);
    System.out.println(student);

    //增删改操作都会清空一级缓存,无论是否提交
    dao.insertStudent(new Student("赵六",26,96.6));

    Student student2 = dao.selectStudentById(2);
    System.out.println(student2);       
}

控制台:

4.PNG

二、内置二级查询缓存

MyBatis查询缓存的作用域是根据映射文件mapper的namespace划分的,相同namespace的mapper查询数据存放在同一个缓存区域。不同namespace下的数据互不干扰。

无论是一级缓存还是二级缓存,都是按照namespace进行分别存放的。但一、二级缓存的不同之处在于,Sqlsession一旦关闭,则Sqlsession中的数据将不存在,即一级缓存就不复存在。而二级缓存的生命周期会与整个应用同步,与Sqlsession是否关闭无关。

使用二级缓存的目的,不是共享数据,因为MyBatis从缓存中读取数据的依据是sql的id,而非查询出的对象。所以,二级缓存中的数据不是为了在多个查询之间共享(所有查询中只要查询结果中存在该对象的,就直接从缓存中读取,这是对数据的共享,hibernate中的缓存就是为了共享,但MyBatis不是),而是为了延长该查询结果的保存时间,提高系统性能。

1.二级缓存用法

二级缓存的使用只需要完成两步:

  • 序列化实体
  • 在mapper映射文件中添加标签

1.实体序列化

要求查询结果所涉及到的实体类要实现java.io.Serializable接口。若该实体类存在父类,或其具有域属性,则父类与域属性类也要实现序列化接口。

public class Student implements Serializable{

    private Integer id;
    private String name;
    private int age;
    private double score;
}

2.mapper映射文件中添加标签

在mapper映射文件中的标签中添加子标签

<mapper namespace="com.hcx.dao.IStudentDao">

    <cache/>

    <select id=selectStudentById resultType="com.hcx.beans.Student">
        select * from student where id=#{id}
    </select>
</mapper>

3.二级缓存的配置

为标签添加一些相关属性设置,可以对二级缓存的运行性能进行控制。若不指定设置,则均保持默认值。

<cache eviction="IFIO" flushInterval="10800000"
       readOnly="true" size="512"/>
  • eviction:逐出策略。当二级缓存中的对象达到最大值时,就需要通过逐出策略将缓存中的对象移出缓存。默认为LRU。常用的策略有FIFO和LRU
  • flushInterval:刷新缓存的时间间隔,单位毫秒。这里的刷新缓存即清空缓存。一般不指定,即当执行增删改时刷新缓存。
  • readOnly:设置缓存中数据是否只读。只读的缓存会给所有调用者返回缓存对象的相同实例,因此这些对象不能被修改,这提供了很重要的性能优势。但读写的缓存会返回缓存对象的拷贝。这会慢一些,但是安全,因此默认是false。
  • size:二级缓存中可以存放的最多对象个数。默认为1024个。

2.二级缓存的存在性证明

对于映射文件中的同一个查询,肯定是同一个namespace中的查询。在一次查询后,将Sqlsession关闭,再进行一次相同查询,发现并没有到DB中进行select查询,说明二级缓存是存在的。

//证明二级缓存的存在
@Test
public void test01(){
    //第一次查询
    Student student = dao.selectStudentById(2);
    System.out.println(student);

    sqlSession.close();

    sqlSession = MyBatisUtils.getSqlSession();
    dao = sqlSession.getMapper(IStudentDao.class);

    //第二次查询
    Student student2 = dao.selectStudentById(2);
    System.out.println(student2);       
}

查看控制台:

Cache Hit Ratio表示缓存命中率。开启二级缓存后,每执行一次查询,系统都会计算一次二级缓存的命中率。第一次查询也是先从缓存中查询,只不过缓存中一定是没有的。所以会再从DB中查询。由于二级缓存中不存在该数据,所以命中率为0.但第二次查询是从二级缓存中读取的,所以这一次的命中率为1/2=0.5。当然,若有第三次查询,则命中率为1/3=0.66

6.PNG

3.增删改对二级缓存的影响

增删改操作,无论是否进行提交sqlSession.commit(),均会清空一级、二级缓存,使查询再次从DB中select。

测试类:

@Test
public void test02(){
    //第一次查询
    Student student = dao.selectStudentById(2);
    System.out.println(student);

    sqlSession.close();

    sqlSession = MyBatisUtils.getSqlSession();
    dao = sqlSession.getMapper(IStudentDao.class);

    //插入
    dao.insertStudent(new Student("",0,0));

    //第二次查询
    Student student2 = dao.selectStudentById(2);
    System.out.println(student2);       
}

控制台:

注意,在第二次查询时的缓存命中率为0.5,但还是从DB中查询了。说明在缓存中与该查询相对应的key是存在的,但其value被清空。而value被清空的原因是前面执行了对DB的增删改操作,所以不会从缓存中直接将null值返回,而是从DB中进行查询。

9.PNG

说明:

  • 二级缓存的清空,实质上是对所查找key对应的value置为null,而非将

三、ehcache二级查询缓存

MyBatis允许使用第三方缓存产品。ehCache就是其中一种。

注意:使用ehcache二级缓存,实体类无需实现序列化接口。

1.导入jar包

jar.PNG

2.添加ehcache.xml

解压ehcache的核心jar包ehcache-core-2.6.8.jar,将其中的一个配置文件ehcache-failsafe.xml直接放到项目的src目录下,并更名为ehcache.xml

(1)标签

指定一个文件目录,当内存空间不够,需要将二级缓存中数据写到硬盘上时,会写到这个指定目录中。其值一般为java.io.tmpdir,表示当前系统的默认文件临时目录。

diskstore.PNG

当前文件系统的默认文件临时目录,可以通过System.property()方法查看:

@Test
public void test(){
    String path = System.getProperty("java.io.tmpdir");
    System.out.println(path);
}

(2)标签

defaultcache.PNG

default1.PNG

3.启用ehcache缓存机制

在映射文件的mapper中的中通过type指定缓存机制为Ehcache缓存。默认为MyBatis内置的二级缓存org.apache.ibatis.cache.impl.PerpetualCache。

<mapper namespace="com.hcx.dao.IStudentDao">

    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>

    <select id=selectStudentById resultType="com.hcx.beans.Student">
        select * from student where id=#{id}
    </select>
</mapper>

4.ehcache在不同mapper中的个性化设置

在ehcache.xml中设置的属性值,会对该项目中所有使用ehcache缓存机制的缓存区域起作用。一个项目中可以有多个mapper,不同的mapper有不同的缓存区域。对于不同缓存区域也可进行专门针对于当前区域的个性设置,可通过指定不同mapper的属性值来设置。

属性值的优先级高于ehcache.xml中的属性值。

<mapper namespace="com.hcx.dao.IStudentDao">

    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
        <property name="maxElementsInMemory" value="5000"/>
        <property name="timeToIdleSeconds" value="240"/>
    </cache>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值