0
点赞
收藏
分享

微信扫一扫

20240418,运算载重符(根本写不对后置递增)

龙驹书房 1天前 阅读 3

缓存机制

1、mybatis缓存

缓存:cache
缓存的作用:通过减少IO的方式,来提高程序的执行效率。
mybatis的缓存:将select语句的查询结果放到缓存(内存)当中,下一次还是这条select语句的话,直接从缓存中取,不再查数据库。一方面是减少了IO。另一方面不再执行繁琐的查找算法。效率大大提升。
mybatis缓存包括:
● 一级缓存:将查询到的数据存储到SqlSession中。
● 二级缓存:将查询到的数据存储到SqlSessionFactory中。
● 或者集成其它第三方的缓存:比如EhCache【Java语言开发的】、Memcache【C语言开发的】等。
缓存只针对于DQL语句,也就是说缓存机制只对应select语句。

2、一级缓存

2.1 开启一级缓存

一级缓存默认是开启的。不需要做任何配置。
原理:只要使用同一个SqlSession对象执行同一条SQL语句,就会走缓存。
测试:

  @Test
    public void test2() throws  Exception{
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectByid(8);
        System.out.println(car);
        Car car1 = mapper.selectByid(8);
        System.out.println(car1);
        sqlSession.close();
    }

在这里插入图片描述

只执行了一次sql语句查询。

2.2 一级缓存失效

什么情况下不走缓存?
● 第一种:不同的SqlSession对象。
● 第二种:查询条件变化了。
一级缓存失效情况包括两种:
● 第一种:第一次查询和第二次查询之间,手动清空了一级缓存。

 @Test
    public void test2() throws  Exception{
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectByid(8);
        System.out.println(car);
        sqlSession.clearCache();
        Car car1 = mapper.selectByid(8);
        System.out.println(car1);
        sqlSession.close();
    }

在这里插入图片描述
第二种:第一次查询和第二次查询之间,执行了增删改操作。【这个增删改和哪张表没有关系,只要有insert delete update操作,一级缓存就失效。】

 @Test
    public void test2() throws  Exception{
        SqlSession sqlSession = MybatisUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectByid(8);
        System.out.println(car);
        mapper.InsertClazz(1003,"四班");
        Car car1 = mapper.selectByid(8);
        System.out.println(car1);
        sqlSession.close();
    }

在这里插入图片描述

3、二级缓存

3.1 开启二级缓存

二级缓存的范围是SqlSessionFactory。
使用二级缓存需要具备以下几个条件:

  1. 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。默认就是true,无需设置。
  2. 在需要使用二级缓存的SqlMapper.xml文件中添加配置:
  3. 使用二级缓存的实体类对象必须是可序列化的,也就是必须实现java.io.Serializable接口
  4. SqlSession对象关闭或提交之后,一级缓存中的数据才会被写入到二级缓存当中。此时二级缓存才可用。
    只有4个同时满足,才会开启二级缓存

Carmapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace先随意写一个-->
<mapper namespace="com.cky.mapper.CarMapper">
<!--    cache 表示当前配置文件 开启二级缓存-->
<cache/>
    <select id="selectByid" resultType="car">
        select * from t_car where id=#{id}
    </select>
</mapper>

CarMapper.JAVA 实现序列化接口

package com.cky.pojo;

import java.io.Serializable;

public class Car implements Serializable {
    private Integer id;
    private String carNum;
    private String brand;
    private Double guidePrice;
    private  String produceTime;
    private String carType;

    public Car(Integer id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {
        this.id = id;
        this.carNum = carNum;
        this.brand = brand;
        this.guidePrice = guidePrice;
        this.produceTime = produceTime;
        this.carType = carType;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCarNum() {
        return carNum;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getGuidePrice() {
        return guidePrice;
    }

    public void setGuidePrice(Double guidePrice) {
        this.guidePrice = guidePrice;
    }

    public String getProduceTime() {
        return produceTime;
    }

    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", carNum='" + carNum + '\'' +
                ", brand='" + brand + '\'' +
                ", guidePrice=" + guidePrice +
                ", produceTime='" + produceTime + '\'' +
                ", carType='" + carType + '\'' +
                '}';
    }
}

测试类

 @Test
    public void test() throws  Exception{
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();

        CarMapper mapper = sqlSession1.getMapper(CarMapper.class);
        CarMapper mapper1 = sqlSession2.getMapper(CarMapper.class);
        Car car = mapper.selectByid(7);
        System.out.println(car);
        //只有关闭sqlsession1 二级缓存才会启用,把一级缓存的内容转移到二级缓存中
        sqlSession1.close();
//        Thread.sleep(10*1000);
        //即使在sleep此期间 我通过数据库软件修改了值,但是由于此时关闭了sqlsession1 二级缓存已经启用,所以再次查询仍然会从缓存中读取,数据库中修改的数据并不会显示。
        Car car1 = mapper1.selectByid(7);
        System.out.println(car1);

        sqlSession2.close();


    }

在这里插入图片描述
第二次并没有执行sql,而是从二级缓存中读取了,因为一级缓存关闭,会将其内容保存在二级缓存中。
如果一级缓存没有关闭,二级缓存不会开启

 @Test
    public void test() throws  Exception{
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();

        CarMapper mapper = sqlSession1.getMapper(CarMapper.class);
        CarMapper mapper1 = sqlSession2.getMapper(CarMapper.class);
        Car car = mapper.selectByid(7);
        System.out.println(car);

//        Thread.sleep(10*1000);
        //即使在sleep此期间 我通过数据库软件修改了值,但是由于此时关闭了sqlsession1 二级缓存已经启用,所以再次查询仍然会从缓存中读取,数据库中修改的数据并不会显示。
        Car car1 = mapper1.selectByid(7);
        System.out.println(car1);
        //只有关闭sqlsession1 二级缓存才会启用,把一级缓存的内容转移到二级缓存中
        sqlSession1.close();
        sqlSession2.close();


    }

在这里插入图片描述
执行了两次。

3.2 二级缓存什么时候失效

进行了增删改的操作,就会导致二级缓存清空,二级缓存此时会失效。
注意:即使增删改的不是不是同一张表,但是只要是同一个sqlsessionfactory创建出来的sqlsession,进行了增删改,都会导致其失效,此时,一级缓存一定也失效

  @Test
    public void test1() throws  Exception{
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        SqlSession sqlSession3 = sqlSessionFactory.openSession();
        CarMapper mapper = sqlSession1.getMapper(CarMapper.class);
        CarMapper mapper1 = sqlSession2.getMapper(CarMapper.class);
        CarMapper mapper2 = sqlSession3.getMapper(CarMapper.class);
        Car car = mapper.selectByid(7);
        System.out.println(car);
        //只有关闭sqlsession1 二级缓存才会启用,把一级缓存的内容转移到二级缓存中
        sqlSession1.close();
        //增操作 ,失效
        int row = mapper2.InsertClazz(1002, "三班");
        sqlSession3.commit();
        System.out.println(row);
        Car car1 = mapper1.selectByid(7);
        System.out.println(car1);

        sqlSession2.close();

        sqlSession3.close();
    }
}

在这里插入图片描述

3.3 二级缓存的相关配置

在这里插入图片描述

  1. eviction:指定从缓存中移除某个对象的淘汰算法。默认采用LRU策略。
    a. LRU:Least Recently Used。最近最少使用。优先淘汰在间隔时间内使用频率最低的对象。(其实还有一种淘汰算法LFU,最不常用。)
    b. FIFO:First In First Out。一种先进先出的数据缓存器。先进入二级缓存的对象最先被淘汰。
    c. SOFT:软引用。淘汰软引用指向的对象。具体算法和JVM的垃圾回收算法有关。
    d. WEAK:弱引用。淘汰弱引用指向的对象。具体算法和JVM的垃圾回收算法有关。
  2. flushInterval:
    a. 二级缓存的刷新时间间隔。单位毫秒。如果没有设置。就代表不刷新缓存,只要内存足够大,一直会向二级缓存中缓存数据。除非执行了增删改。
  3. readOnly:
    a. true:多条相同的sql语句执行之后返回的对象是共享的同一个。性能好。但是多线程并发可能会存在安全问题。
    b. false:多条相同的sql语句执行之后返回的对象是副本,调用了clone方法。性能一般。但安全。
  4. size:
    a. 设置二级缓存中最多可存储的java对象数量。默认值1024。

4、MyBatis集成EhCache

集成EhCache是为了代替mybatis自带的二级缓存。一级缓存是无法替代的。
mybatis对外提供了接口,也可以集成第三方的缓存组件。比如EhCache、Memcache等。都可以。
EhCache是Java写的。Memcache是C语言写的。所以mybatis集成EhCache较为常见,按照以下步骤操作,就可以完成集成:
第一步:引入mybatis整合ehcache的依赖。

<!--mybatis集成ehcache的组件-->
<dependency>
  <groupId>org.mybatis.caches</groupId>
  <artifactId>mybatis-ehcache</artifactId>
  <version>1.2.2</version>
</dependency>

第二步:在类的根路径下新建echcache.xml文件,并提供以下配置信息。

<?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">
    <!--磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存-->
    <diskStore path="e:/ehcache"/>
  
    <!--defaultCache:默认的管理策略-->
    <!--eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断-->
    <!--maxElementsInMemory:在内存中缓存的element的最大数目-->
    <!--overflowToDisk:如果内存中数据超过内存限制,是否要缓存到磁盘上-->
    <!--diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false-->
    <!--timeToIdleSeconds:对象空闲时间(单位:秒),指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问-->
    <!--timeToLiveSeconds:对象存活时间(单位:秒),指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问-->
    <!--memoryStoreEvictionPolicy:缓存的3 种清空策略-->
    <!--FIFO:first in first out (先进先出)-->
    <!--LFU:Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存-->
    <!--LRU:Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存-->
    <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
                  timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>

</ehcache>

第三步:修改SqlMapper.xml文件中的标签,添加type属性。

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

测试类:跟之前没区别

 @Test
    public void test() throws  Exception{
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();

        CarMapper mapper = sqlSession1.getMapper(CarMapper.class);
        CarMapper mapper1 = sqlSession2.getMapper(CarMapper.class);
        Car car = mapper.selectByid(7);
        System.out.println(car);
        sqlSession1.close();
//        Thread.sleep(10*1000);
        //即使在sleep此期间 我通过数据库软件修改了值,但是由于此时关闭了sqlsession1 二级缓存已经启用,所以再次查询仍然会从缓存中读取,数据库中修改的数据并不会显示。
        Car car1 = mapper1.selectByid(7);
        System.out.println(car1);
        //只有关闭sqlsession1 二级缓存才会启用,把一级缓存的内容转移到二级缓存中

        sqlSession2.close();


    }

在这里插入图片描述

举报

相关推荐

0 条评论