Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MyBatis caching mechanism


May 16, 2021 MyBatis


Table of contents


The caching mechanism relieves database pressure and improves database performance

Mybatis's cache is divided into two levels: first-level cache and second-level cache

Level 1 cache:

The first-level cache sqlsesson cache, and the cached data is valid only within SqlSession. You need to create a SqlSession session object when you manipulate the database, and there is a HashMap in the object that stores cached data, which is private to the current session object and inoperable to other SqlSession session objects.

Process:

The first execution of select is completed and the data found is cached in HashMap within SqlSesion

The second execution of select will look up the data from the cache, and if the select is the same as the pass parameters, the data can be returned from the cache without going to the database, thus increasing efficiency

Attention:

1. If SqlSession performs a DML operation (insert, update, delete) and commits, mybatis will empty all cached data in the current SqlSession cache, which ensures that the data stored in the cache is always consistent with the database and avoids differences

2. When a SqlSession ends, then the first-level cache inside him does not exist, mybatis default is to turn on the first-level cache, do not need to be configured

3, mybatis cache is based on the "namespace:sql statement: parameters" to cache, meaning that sqlSession's HashMap stores cached data, is used "namespace:sql:parameters" as key, the query returned statements as value saved

Secondary cache:

A secondary cache is mapper cache, that is, a mapper.xml of the same namespace, and when multiple SqlSessions operate the database using the same Mapper, the resulting data is cached in the same secondary cache area

The secondary cache is not turned on by default. The second-level cache needs to be configured in the set global parameters

To turn on the secondary caching step:

1, conf.xml of global variables to open the secondary cache

<settings>
<setting name="cacheEnabled" value="true"/>默认是false:关闭二级缓存
<settings>

2, in userMapper.xml configuration

<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>当前mapper下所有语句开启二级缓存

An LRU cache is configured here and refreshed every 60 seconds, with a maximum storage of 512 objects, while the returned objects are read-only

To disable the secondary cache of the current select statement, add useCache="false" follows:

<select id="getCountByName" parameterType="java.util.Map" resultType="INTEGER" statementType="CALLABLE" useCache="false">

Process:

1. When sqlseesion select this session is session results are cached to the secondary cache

2. When another sqlsession select it will first look in his own first-level cache, if not found, go back to the second-level cache, find it and return, without going to the database, thereby reducing database pressure and improving performance

Attention:

1. If SqlSession a DML (insert、update、delete) and commit mybatis will empty all mapper cache, which ensures that the stored data in the cache is always consistent with the database and avoids differences

2, mybatis cache is based on [namespace:sql语句:参数] to cache, which means that [namespace:sql:参数] SqlSession HashMap stores cached data, it is saved as value using the statement returned by the value key