mybatis学习笔记11----缓存的简单理解

13.mybatis缓存

1.缓存:存放在内存中的临时数据,用户从缓存中读取,提高查询的效率,解决了高并发系统的性能问题。

2.使用缓存:

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

3.什么时候用缓存?

经常查询并且不经常改变的数据。(可以使用缓存)

13.2mybatis的缓存

mybatis可以非常方便的定制和配置缓存。缓存可以大大的提高系统的查询效率。

mybatis分为一级缓存和二级缓存

  • 默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存 )

  • 二级缓存需要手动开启和配置,它是基于namespace级别的缓存。

  • 为了有更好的扩展性,mybatis定义了缓存的接口Cache,我们可以通过实现Cache接口来定义二级缓存。

13.3一级缓存(默认开启)
 <select id="queryUserId" parameterType="_int" resultType="user">
        select * from user where id=#{id}
    </select>
 @Test
    public void test1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.queryUserId(1);
        System.out.println(user1);

        System.out.println("**********");

        User user2=mapper.queryUserId(1);
        System.out.println(user2);

        System.out.println(user1==user2);
        sqlSession.close();

    }

当同时查询两次同一个用户时,SqlSession只查询一次,且将他们相等判断,返回值为true。

说明了它第二次是从缓存中读取的数据,默认开启了一级缓存。

缓存失效的情况:

  • 查询不同的东西。
  • 增删改操作,可能会改变原来的数据,所以必须会刷新缓存。
  • 查询不同的Mapper.xml
  • 手动清理缓存。
 sqlSession.clearCache();//清理缓存

清理缓存之后会执行两次SQL。返回为flase

查询相同的时候,用户就会从缓存中获取,一级缓存相当于Map,用的时候取,不能就关掉。

13.4二级缓存

什么是二级缓存?

  • 二级缓存也叫全局缓存,一级缓存的作用域太低了,所以诞生了二级缓存。
  • 基于namespace级别的缓存,一个名称空间,对应一个二级缓存。
  • 工作机制
    • 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中。
    • 如果当前会话关闭了,这个会话对应的一级缓存就没了,但是我们开启了二级缓存,所以它会把一级缓存的东西保存到二级缓存。
    • 新的会话查询信息,就可以在二级缓存中获取信息。
    • 不同的mapper查出的数据会放在自己对应的缓存中。

步骤:

1.开启缓存

 <!--显示的开启缓存-->
    <setting name="cacheEnabled" value="true"/>

2.在要使用的Mapper.xml中开启二级缓存

 <!--在当前的mapper.xml中使用二级缓存-->
    <cache  eviction="FIFO"
            flushInterval="60000"
            size="512"
            readOnly="true"/>

这个eviction更高级的配置创建了一个 FIFO 缓存,flushInterval每隔 60 秒刷新,最多可以存储结果对象或列表的 512 个引用,而且readOnly返回的对象被认为是只读的,因此对它们进行修改可能会在不同线程中的调用者产生冲突。

或者,直接开启二级缓存不设置参数

<cache />

3.测试

@Test
    public  void Test2Cache(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserId(1);
        System.out.println(user);
        sqlSession.close();

        SqlSession sqlSession2 = MybatisUtils.getSqlSession();
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        User user2 = mapper2.queryUserId(1);
        System.out.println(user2);
        sqlSession2.close();
    }
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Cache Hit Ratio [com.kuang.dao.UserMapper]: 0.0
Opening JDBC Connection
Created connection 323326911.
==>  Preparing: select * from user where id=?
==> Parameters: 1(Integer)
<==    Columns: id, name, pwd
<==        Row: 1, 李一, 123456
<==      Total: 1
User(id=1, name=李一, pwd=123456)
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@134593bf]
Returned connection 323326911 to pool.
Cache Hit Ratio [com.kuang.dao.UserMapper]: 0.5
User(id=1, name=李一, pwd=123456)

Process finished with exit code 0

从日志可以看出,当开启二级缓存时,两个对象同时去查询一个东西。一个对象先查询后关闭,会把读取的内容放到二级缓存。第二次在读取相同的内容时,会直接从二级缓存读取。

出现的错误

org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableException: com.kuang.pojo.User

User实体类没有序列化

解决:

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

继承Serializable类,实现序列化

小结:
  • 只要开启了二级缓存,在同一个Mapper下就有效
  • 所有的数据都会先放在一级缓存中。
  • 只有当会话提交,或者关闭时,才会提交到二级缓存。
  • 用户读取时,先读取二级缓存,如果二级缓存没有,则读取一级缓存,一级缓存没有,则读取数据库,放到一级缓存。如果有就直接读取二级缓存或者一级缓存。
13.5自定义缓存Ehcache

1.导包

  <!--mybatis ehcache自定义缓存-->
      <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.1.0</version>
</dependency>

2.Mapper.xml配置

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

配置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"/>
    <!--
defaultCache默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略,只能定义一个
            eternal对象是否永久有效,一旦设置了timeout就不起作用。
            maxElementsInMemory 缓存的最大数目
            overflowToDisk=是否保存到磁盘,当系统死机。
            diskPersistent=是否缓存虚拟机启动数据
            timeToIdleSeconds=设置对象在失效前的允许闲置时间
            timeToLiveSeconds="设计对象在失效前允许存活的时间
            memoryStoreEvictionPolicy=可选策略LRU,FIFO,LFU
    -->
</ehcache>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值