MyBatis study concluded (7) - Mybatis cache

A, MyBatis cache description

  As with most persistence layer frameworks, MyBatis also provides a cache and secondary cache support

  1. A cache : a HashMap based PerpetualCache local cache, which stores a scope of  the Session , when the  Session flush or close  after the all of the Cache Session will be emptied .

  2. The  secondary cache and cache the same mechanism, but also using default PerpetualCache, HashMap stored, in that different storage scoped Mapper (the Namespace) , and can be custom storage source , such as Ehcache.

  3. For a data cache update mechanism, when performing the C / U / D operation of a certain scope (a cache Session / secondary cache the Namespaces) after default select the scope of all the cache will be clear.

1.1, Mybatis cache test

Copy the code
 1 package me.gacl.test;
 2 
 3 import me.gacl.domain.User;
 4 import me.gacl.util.MyBatisUtil;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.junit.Test;
 7 
 8 /**
 9  * @author gacl
10  * 测试一级缓存
11  */
12 public class TestOneLevelCache {
13     
14     /*
15      * 一级缓存: 也就Session级的缓存(默认开启)
16      */
17     @Test
18     public void testCache1() {
19         SqlSession session = MyBatisUtil.getSqlSession();
20         String statement = "me.gacl.mapping.userMapper.getUser";
21         User user = session.selectOne(statement, 1);
22         System.out.println(user);
23         
24         /*
25          * 一级缓存默认就会被使用
26          */
27         user = session.selectOne(statement, 1);
28         System.out.println(user);
29         session.close();
30         /*
31          1. 必须是同一个Session,如果session对象已经close()过了就不可能用了 
32          */
33         session = MyBatisUtil.getSqlSession();
34         user = session.selectOne(statement, 1);
35         System.out.println(user);
36         
37         /*
38          2. 查询条件是一样的
39          */
40         user = session.selectOne(statement, 2);
41         System.out.println(user);
42         
43         /*
44          3. 没有执行过session.clearCache()清理缓存
45          */
46         //session.clearCache(); 
47         user = session.selectOne(statement, 2);
48         System.out.println(user);
49         
50         /*
51          4. 没有执行过增删改的操作(这些操作都会清理缓存)
52          */
53         session.update("me.gacl.mapping.userMapper.updateUser",
54                 new User(2, "user", 23));
55         user = session.selectOne(statement, 2);
56         System.out.println(user);
57         
58     }
59 }
Copy the code

1.2, Mybatis two cache test

  1, open the second-level cache, add the following configuration file in userMapper.xml

<mapper namespace="me.gacl.mapping.userMapper">
<!-- 开启二级缓存 -->
<cache/>

  2, the secondary cache test

Copy the code
 1 package me.gacl.test;
 2 
 3 import me.gacl.domain.User;
 4 import me.gacl.util.MyBatisUtil;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 import org.junit.Test;
 8 
 9 /**
10  * @author gacl
11  * 测试二级缓存
12  */
13 public class TestTwoLevelCache {
14     
15     /*
16      * 测试二级缓存
17      * 使用两个不同的SqlSession对象去执行相同查询条件的查询,第二次查询时不会再发送SQL语句,而是直接从缓存中取出数据
18      */
19     @Test
20     public void testCache2() {
21         String statement = "me.gacl.mapping.userMapper.getUser";
22         SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
23         //开启两个不同的SqlSession
24         SqlSession session1 = factory.openSession();
25         SqlSession session2 = factory.openSession();
26         //使用二级缓存时,User类必须实现一个Serializable接口===> User implements Serializable
27         User user = session1.selectOne(statement, 1);
28         session1.commit();//不懂为啥,这个地方一定要提交事务之后二级缓存才会起作用
29         System.out.println("user="+user);
30         
31         //由于使用的是两个不同的SqlSession对象,所以即使查询条件相同,一级缓存也不会开启使用
32         user = session2.selectOne(statement, 1);
33         //session2.commit();
34         System.out.println("user2="+user);
35     }
36 }
Copy the code

1.3, the secondary cache supplement

  1. All select statements in the mapped statement file will be cached.

  All insert 2. mapped statement file, update and delete statements will flush the cache.

  3. Cache will use the Least Recently Used (LRU, least recently used) algorithm for eviction.

  4. The cache is refreshed according to a specified time interval.

  The cache stores objects 1024

cache tag common attributes:

<cache 
eviction="FIFO"  <!--回收策略为先进先出-->
flushInterval="60000" <!--自动刷新时间60s-->
size="512" <!--最多缓存512个引用对象-->
readOnly="true"/> <!--只读-->

 

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/606256

Guess you like

Origin blog.csdn.net/weixin_33892359/article/details/92659974