Keep in mind MyBatis’s first-level, second-level cache and serialization issues

A cache is a space in memory that is usually used to improve query efficiency. MyBatis supports two cache technologies: first-level cache and second-level cache. The first-level cache is enabled by default, and the second-level cache is disabled by default.

Level 1 cache:

  1. Level 1 cache is enabled by default

  2. It is a cache based on SqlSession, the same SqlSession is valid, and different SqlSession is invalid

  3. The same id can go to the cache; different ids, even if the query content is the same, the cache will not be taken. The sample code is as follows:

  • The sample code is as follows:

    //Load the core configuration file
    InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
    //Create a factory object based on the builder
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    //Create a sqlSession object through the factory: test the first-level cache
    //first query
    SqlSession sqlSession1 = factory. openSession();
    List<User> list1 = sqlSession1. selectList("selAll1");
    System.out.println(list1);
    System.out.println("**************************");
    //Second query
    List<User> list2 = sqlSession1. selectList("selAll1");
    System.out.println(list2);
    sqlSession1. close();
    

    mybatis first level cache

  1. //Load the core configuration file
    InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
    //Create a factory object based on the builder
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    //Create a sqlSession object through the factory: test the first-level cache
    SqlSession sqlSession1 = factory. openSession();
    List<User> list1 = sqlSession1. selectList("selAll1");
    System.out.println(list1);
    System.out.println("************Different id, even if the content is the same, do not use the cache**************");
    List<User> list2 = sqlSession1. selectList("selAll2");
    System.out.println(list2);
    sqlSession1. close();
    
  2. mybatis level 1 cache
    //load core configuration file
    InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
    //Create a factory object based on the builder
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    //Create a sqlSession object through the factory: test the first-level cache
    SqlSession sqlSession1 = factory. openSession();
    List<User> list1 = sqlSession1. selectList("selAll1");
    System.out.println(list1);
    // sqlSession1. close();
    System.out.println("************ is not the same sqlSession, will not go to cache**************");
    SqlSession sqlSession2 = factory. openSession();
    List<User> list2 = sqlSession2. selectList("selAll1");
    // System.out.println(list2);
    

    mybatis first level cache

  • Level 2 cache:

  1. L2 cache is disabled by default

  2. It is a cache based on SqlSessionFactory. SqlSession created by the same factory is valid, but not valid by different factories.

  3. When it needs to be used, it should be opened under the specified namespace

  4. Attributes of the tag

  • eviction: cache clearing strategy, commonly used are LRU, FIFO, SOFT, WEAK

  • flushInterval: Refresh interval, a number of milliseconds needs to be given, indicating that the cache will be automatically refreshed when the time interval is specified

  • size: Set the size, the default is 1024

  • readOnly: whether it is read-only (do not write to the file), the default is false, and the entity class needs to implement the serialization interface

  • type: used to customize the caching mechanism, providing a fully qualified path of a custom class, which needs to implement the CaChe interface

  • type: used to customize the caching mechanism, providing a fully qualified path of a custom class, which needs to implement the CaChe interface

//Load the core configuration file
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
//Create a factory object based on the builder
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//Create a sqlSession object through the factory: test the second-level cache
SqlSession sqlSession1 = factory. openSession();
 
List<User> list1 = sqlSession1. selectList("selAll1");
System.out.println(list1);
sqlSession1. close();
 
System.out.println("************The sqlSession of the same factory will go to the cache****************");
SqlSession sqlSession2 = factory. openSession();
List<User> list2 = sqlSession2. selectList("selAll1");
System.out.println(list2);

L2 cache

cache hit ratio means cache hit ratio in Chinese.

This is a computer term. When an end user accesses an acceleration node, if the node has cached the data to be accessed, it is called a hit. If not, it needs to go back to the original server to fetch it, which means there is no hit.

Serialization problem:

//Load the core configuration file
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
//Create a factory object based on the builder
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//Create a sqlSession object through the factory: test the second-level cache
SqlSession sqlSession1 = factory. openSession();
 
List<User> list1 = sqlSession1. selectList("selAll1");
System.out.println(list1);
sqlSession1. close();

serialization problem

Cause analysis: The User implementation class does not implement serialization.

Why implement serialization: (1) Cache mechanism: save query results in memory (2) When the memory is full and needs to be removed, MyBatis will automatically remove the content in memory, but the file is very important and cannot be deleted. At this time, serialization is required to save the content from the memory to the hard disk in the form of a file, and the reading and writing of a content saved as a file must be serialized.

solution:

One: Serialization of the User entity class

Serialization

Two: Add the readOnly attribute to the tag

Indicates: Mybatis is required to only read and not write the cached content. When it needs to be removed, delete it directly without dumping.