Mybatis(缓存+ehcache)

什么是缓存?

存在内存中的临时数据(只读)
将用户经常查询的数据放在缓存中,用户去查询数据就不用从磁盘上(关系型数据库文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。

为什么使用缓存?

减少和数据库的交互次数,减少系统开销,提高系统效率。

什么样的数据能使用缓存?

经常查询并且不经常改变的数据

缓存顺序

第一次查询先看二级缓存有没有,再看一级有没有,没有走数据库,然后放在一级缓存中,之后走缓存。

Mybatis缓存

Mybatis包含一个非常强大的查询缓存特性,它可以非常便利的定制和配置缓存。缓存可以极大的提升查询效率。
Mybatis系统中默认定义了两级缓存:一级缓存二级缓存
  默认情况下,只有一级缓存开启。(SqlSession级别缓存,也成为本地缓存)
  二级缓存需要手动开启和配置,他是基于namespace级别的缓存。
  为了提高扩展性,Mybatis定义了缓存接口Cache。我们可以通过实现Cache接口来定义二级缓存。

一级缓存

一级缓存也叫本地缓存:SqlSession
  与数据库同一次绘画期间查询到的数据会放在本地缓存中。
  如果需要获取相同的数据,直接从缓存中拿,没必要再去查询数据库;
在这里插入图片描述

测试步骤

1.开启事务日志
在这里插入图片描述

2.测试在一个Session中查询两次相同记录
3.查看日志输出

缓存失效的情况

1.查询不同内容
2.增删改操作,可能会 改变原来的数据
3.查询不同Mapper.xml(跃出作用域)
4.手动清理缓存
小结:一级缓存默认是开启的,只在一次SqlSession中有效,也就是拿到连接到关闭连接这个区间段。

在这里插入图片描述

二级缓存

二级缓存也叫全局缓存,一级缓存作用域太低了,所以有二级缓存。
基于namespace级别的缓存,一个名称空间,对应一个二级缓存
工作机制:
  一个会话查询一条数据,这个数据会被放在当前会话的一级缓存中。
  如果当前会话关闭了,这个会话对应的一级缓存就没了。但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中。
  新的会话查询消息,就可以从二级缓存中获取内容。
  不同的mapper查询的数据会放在自己的对应的缓存(map)中。
在这里插入图片描述

测试步骤

在这里插入图片描述
1. 开启全局缓存
< setting name=“cacheEnabled” value=“true”/>
2.在当前Mapper.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">
<mapper namespace="com.my.dao.UserMapper">
    <!--在当前Mapper.xml中使用二级缓存-->
   <cache/>
<cache/>
</mapper>
方法二:
<?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">
<mapper namespace="com.my.dao.UserMapper">
    <!--自定义参数-->
   <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
<cache/>
</mapper>

3.测试
在这里插入图片描述
方法一的问题:需要将实体类序列化,否则报错
在这里插入图片描述

@Data
public class User implements Serializable {
    
    
    private int id;
    private String name;
    private String pwd;
}

小结:
  只要开启缓存就在同一个Mapper下有效,第一个sqlSession归还内存时候,会到namespace位置,这样第二个sqlSession会从这里获得缓存。
  所有数据都会先放在一级缓存。只有会话提交后,才会到二级。

ehcache

在这里插入图片描述

使用ehcache

1.导包

 <dependency>
        <groupId>org.mybatis.caches</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.2.1</version>
    </dependency>

2.Mapper.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">
<mapper namespace="com.my.dao.UserMapper">
    <!--在当前Mapper.xml中使用ehcache-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
</mapper>

3.resources包下ehcache.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">

    <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>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/fhuqw/article/details/121252614