cache
13.1 Introduction
1. What is cache [Cache]
Temporary data stored in memory
Put the data frequently queried by the user in the cache (memory), so that the user does not need to query from the disk (relational database data file) to query the data, but from the cache, thereby improving query efficiency
2. Why use caching
Reduce the number of interactions with the database, reduce system overhead, and improve system efficiency
3. What kind of data can use cache?
Data that is frequently queried and infrequently changed
13.2, Mybatis cache
Mybatis contains a very powerful query cache feature, which can be very convenient to customize and configure the cache, and the cache can greatly improve query efficiency
Two levels of cache are defined by default in the Mybatis system: level one cache and level two cache
By default, only the first level cache is enabled, (SqlSession level cache, also known as local cache)
The second-level cache needs to be manually enabled and configured. It is based on the namespace-level cache
In order to improve scalability, Mybatis defines the cache interface Cache, and we can customize the second-level cache by implementing the Cache interface
13.3 Level 1 cache
Level 1 cache is also called local cache: SqlSession
The data queried during the same session with the database will be placed in the local cache
If you need to get the same data in the future, you can get it directly from the cache, and you don’t have to query the database again
Test steps:
1. Turn on the log
2. Test to query the same record twice in a Session
@Test
public void test1() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
System.out.println("=====================================");
User user2 = mapper.getUserById(1);
System.out.println(user2 == user);
}
3. Check the log output
Cache invalidation:
-
query for different things
-
Addition, deletion and modification operations may change the original data, so the cache must be refreshed
-
Query different Mapper.xml
-
Manually clear the cache
sqlSession.clearCache();
13.4, L2 cache
The second-level cache is also called the global cache. The scope of the first-level cache is too low, so the second-level cache was born
Cache based on namespace level, a namespace corresponds to a second-level cache
Working Mechanism:
When a session queries a piece of data, the data will be placed in the first-level cache of the current session
If the current session is closed, the first-level cache corresponding to this session will be gone; but what we want is that when the session is closed, the data in the first-level cache will be saved to the headphone cache
The new session query information can be obtained from the secondary exchange
The data queried by different mappers will be placed in their corresponding cache (map)
Level 1 cache enabled (SqlSession level cache, also known as local cache)
The second-level cache needs to be manually enabled and configured. It is based on the namespace-level cache
In order to improve scalability, Mybatis defines the cache interface Cache, we can define the cache by implementing the Cache interface
step:
1. Turn on the global cache
<!--显示的开启全局缓存-->
<setting name="cacheEnabled" value="true"/>
2. Use cache in Mapper.xml
<!--在当前Mapper.xml中使用二级缓存-->
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
3. Test
1. Problem: We need to serialize the entity class, otherwise an error will be reported
summary:
As long as the secondary cache is enabled, it will be valid under the same mapper
All data will be placed in the first level cache
Only when the current session is submitted or closed, will it be submitted to the second-level cache
13.5 Caching principle
Notice:
Only queries have caching, depending on whether the data needs to be cached (whether the modification is frequent or not) useCache="true"
<select id="getUserById" resultType="user" useCache="true">
select * from user where id = #{id}
</select>
13.6 Custom cache - ehcache
Ehcache is a widely used open source Java distributed cache. Primarily for general purpose caching
1. Guide package
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
2. Specify to use our ehcache cache implementation in mapper
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>