Open mybatis to open the secondary cache

There are first-level cache and second-level cache in Mybatis. By default, the first-level cache is turned on and cannot be turned off. The first level cache refers to the SqlSession level cache. When the same SQL statement query is performed in the same SqlSession, the second and subsequent queries will not be queried from the database, but directly obtained from the cache. The first level cache can cache up to 1024 Article SQL. The second-level cache refers to a cache that can span SqlSessions. It is a mapper-level cache. For mapper-level caches, different sqlsessions can be shared. The mapper creates a cache data structure in the unit of a namespace and needs to be manually opened.

1. Turn on the secondary cache

Unlike the first level cache that is enabled by default, the second level cache needs to be manually enabled

1. Turn on the cache

First, add the following code to the global configuration file mybatis-configuration.xml file:

<!--开启二级缓存  -->
<settings>    
     <setting name="cacheEnabled" value="true"/>
</settings>

Springboot configuration to enable secondary cache

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.entity
  configuration:
    cache-enabled: true

2. Use cache in XXXmapper.xml file

<!-- 开启二级缓存 -->
<cache></cache>

We can see that there is just such an empty tag in the mapper.xml file. In fact, it can be configured here. The PerpetualCache class is the class that mybatis implements the cache function by default. We do not write the type and use the default cache of mybatis, or we can implement the Cache interface to customize the cache.

    <cache type="org.apache.ibatis.cache.impl.PerpetualCache">
        <property name="eviction" value="LRU" />
        <property name="flushInterval" value="6000000" />
        <property name="size" value="1024" />
        <property name="readOnly" value="false" />
    </cache>

Two, useCache and flushCache

1.
UseCache Mybatis can also configure configuration items such as userCache and flushCache, userCache is used to set whether to disable the second-level cache, useCache=false disables the second-level cache of the current select statement, that is, every query will issue sql to query, the default The situation is true, that is, the SQL uses the second-level cache.

#禁用缓存
<select id="selectUserByUserId" useCache="false" resultType="com.ys.twocache.User" parameterType="int">    
select * from user where id=#{id}
</select>

For each query, the latest data sql is required. Set useCache=false, disable the second-level cache, and obtain it directly from the database.

In the same namespace of the mapper, if there are other insert, update, and delete operations, the cache needs to be refreshed, and dirty reads will occur if the cache is not refreshed.

2、flushCache

flushCache=”true”, it is true by default, that is, the cache is refreshed, if it is changed to false, it will not be refreshed. When using the cache, if you manually modify the query data in the database table, dirty reads will occur.

#flushCache="true" 刷新缓存,flushCache="false" 不刷新缓存,可能出现脏读
<select id="selectUserByUserId" flushCache="true" useCache="false" resultType="com.ys.twocache.User" parameterType="int">    
select * from user where id=#{id}
</select>

Generally, the cache needs to be refreshed after the commit operation is executed. flushCache=true indicates that the cache is refreshed, which can avoid dirty reading of the database.
So we don't need to set, the default is fine.

3. About @CacheNamespace

@CacheNamespace annotation is mainly used for mybatis second-level cache, which is equivalent to attributes. But pay attention to:

@CacheNamespace Although the xml configuration and annotation functions are basically the same, you should pay attention to the following when using @CacheNamespace:

Configuration files and interface comments cannot be used together. It can only be used in the form of full annotations or all through the xml configuration file, and the use of the secondary cache will cause problems, such as not refreshing the cache after updating the data

See the example below:

//通过注解的方式使用二级缓存
@CacheNamespace(implementation = MybatisRedisCache.class)
public interface UserMapper(
    @Select("select * from t_user where user_id = #{userId}")
    @Options(useCache = true)
    List<User> getUser(User u);
}
//通过配置文件的方式,使用二级缓存
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mybatis.UserMapper">
 
    <cache type="cn.mybatis.MybatisRedisCache">
        <property name="eviction" value="LRU" />
        <property name="flushInterval" value="6000000" />
        <property name="size" value="1024" />
        <property name="readOnly" value="false" />
    </cache>
 
    <select id="selectById">
        select * from test where id = #{id}
    </select >
</mapper>

Fourth, the limitations of mybatis secondary cache

The granularity of mybatis secondary cache is not fine enough. It caches in the unit of mapper. For example, if all products are cached, but only one of them is updated, then all caches will be refreshed. At this time, other methods can be used. Caching, such as third-party cache redis, etc.
Insert picture description here

Reference link:
http://www.mybatis.cn/archives/128.html
https://www.cnblogs.com/charlypage/p/9747145.html

Guess you like

Origin blog.csdn.net/u011582840/article/details/108105540