美文网首页
Springboot结合 Mybatis 管理一二级缓存技术

Springboot结合 Mybatis 管理一二级缓存技术

作者: JohnYuCN | 来源:发表于2021-07-29 16:17 被阅读0次

    一、默认整合后的状态:

    1. 与session相关的一级缓存打开
    2. 与sessionFactory相关的二级缓存关闭

    二、调整及测试方式:

    1. 一级缓存:

    (1)默认值:session级,因为session的开闭是与事务相关的,同时项目的每个Mapper接口中的方法即为一个事务,这样相当于sessoin在mapper每次curd操作都会打开和关闭。此时业务方法中如果没有事务声明,则无法使用缓存数据。
    (2)常用的对策:将事务设计在业务方法中,此时session的开闭就等同设置到了业务方法的边界上,这此在一个业务的多次mapper调用,就可以使用缓存的数据了。
    (3)可选值:statement,相当于放弃一级缓存,此时即使同一个session的curd操作,都会直接操作数据库。
    (4)具体操作的方法:

    在applicaiton.yml中:

    mybatis:
      configuration:
        local-cache-scope: session|statement
    

    (5) 测试方法:
    加入日志打印配置:application.yml

    logging:
      level:
        cn:
          johnyu:
            mapper: trace
    

    业务类,利用同一个id,调用了两次mapper:

    @Component
    // @Transactional: 加入和不加入两种选择
    public class EmpServiceImpl implements EmpService {
        @Autowired
        private EmployeeMapper mapper;
        @Override
        public Employee load(int id) {
            Employee employee = mapper.load(id);
            Employee employee1 = mapper.load(id);
            System.out.println(employee==employee1);
            return employee;
        }
    

    此时对业务进行调用后的日志结果如下:

    • 业务无事务&& session情况下,结论:无法在业务中使用缓存数据,进行了两次查询,并且对象不相同!
    2021-07-29 15:48:40.571 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 15:48:40.591 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 15:48:40.604 TRACE 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 15:48:40.605 TRACE 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 15:48:40.606 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    2021-07-29 15:48:40.611 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 15:48:40.611 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 15:48:40.612 TRACE 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 15:48:40.612 TRACE 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 15:48:40.612 DEBUG 32295 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    false
    
    • 业务加入事务&& session情况下,结论:可以在业务中使用缓存数据,仅进行了一次查询,并且对象相同!
    2021-07-29 15:51:50.703 DEBUG 32325 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 15:51:50.722 DEBUG 32325 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 15:51:50.736 TRACE 32325 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 15:51:50.736 TRACE 32325 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 15:51:50.738 DEBUG 32325 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    true
    
    • 无论业务是否加入事务&& statement情况下,结论:无缓存数据
    2021-07-29 15:53:27.566 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 15:53:27.585 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 15:53:27.599 TRACE 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 15:53:27.599 TRACE 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 15:53:27.600 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    2021-07-29 15:53:27.604 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 15:53:27.604 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 15:53:27.605 TRACE 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 15:53:27.605 TRACE 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 15:53:27.605 DEBUG 32342 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    false
    

    2、二级缓存:

    默认情况下,二级缓存是不开放的,如果二缓存打开,则可以跨session获取缓存在sessionFactory中的数据。
    (1)打开的方法:
    在applicaiton.yml中:

    mybatis:
      configuration:
        cache-enabled: true
    

    同时,在需要缓存的Mapper的映射文件中,加入<cache/>:

    <mapper namespace="cn.johnyu.mapper.EmployeeMapper">
        <cache/>
        ...
    

    (2) 测试:

    • 不开启业务的事务 && session 情况:使用了二级缓存,但对象不为同一个。
    2021-07-29 16:02:58.618 DEBUG 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 16:02:58.638 DEBUG 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 16:02:58.652 TRACE 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 16:02:58.652 TRACE 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 16:02:58.653 DEBUG 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    2021-07-29 16:02:58.663  WARN 32435 --- [nio-8080-exec-1] o.apache.ibatis.io.SerialFilterChecker   : As you are using functionality that deserializes object streams, it is recommended to define the JEP-290 serial filter. Please refer to https://docs.oracle.com/pls/topic/lookup?ctx=javase15&id=GUID-8296D8E8-2B93-4B9A-856E-0A65AF9B8C66
    2021-07-29 16:02:58.664 DEBUG 32435 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper          : Cache Hit Ratio [cn.johnyu.mapper.EmployeeMapper]: 0.5
    false
    
    • 开启了业务的事务 && session:虽然使用了二级缓存,但因为一级缓存数据优先使用,对象为同一个
    2021-07-29 16:07:48.177 DEBUG 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper          : Cache Hit Ratio [cn.johnyu.mapper.EmployeeMapper]: 0.0
    2021-07-29 16:07:48.184 DEBUG 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 16:07:48.204 DEBUG 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 16:07:48.218 TRACE 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 16:07:48.218 TRACE 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 16:07:48.219 DEBUG 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    2021-07-29 16:07:48.223 DEBUG 32480 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper          : Cache Hit Ratio [cn.johnyu.mapper.EmployeeMapper]: 0.0
    true
    
    
    • 无论是否开启了业务的事务 && statement:放弃使用一二级缓存,直接命中数据库
    2021-07-29 16:12:53.762 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 16:12:53.782 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 16:12:53.796 TRACE 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 16:12:53.796 TRACE 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 16:12:53.797 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    2021-07-29 16:12:53.801 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper          : Cache Hit Ratio [cn.johnyu.mapper.EmployeeMapper]: 0.0
    2021-07-29 16:12:53.802 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==>  Preparing: select empno as id,ename,job from emp where empno=?
    2021-07-29 16:12:53.802 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : ==> Parameters: 7499(Integer)
    2021-07-29 16:12:53.803 TRACE 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==    Columns: id, ename, job
    2021-07-29 16:12:53.803 TRACE 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==        Row: 7499, ALLEN, SALESMAN
    2021-07-29 16:12:53.803 DEBUG 32528 --- [nio-8080-exec-1] cn.johnyu.mapper.EmployeeMapper.load     : <==      Total: 1
    false
    

    相关文章

      网友评论

          本文标题:Springboot结合 Mybatis 管理一二级缓存技术

          本文链接:https://www.haomeiwen.com/subject/bnzxvltx.html