Home  >  Article  >  Java  >  What is query cache? Introduction to the usage of MyBatis query cache

What is query cache? Introduction to the usage of MyBatis query cache

零下一度
零下一度Original
2017-06-25 10:56:072357browse

Please indicate the source for reprinting:

As mentioned earlier: Spring+SpringMVC+MyBatis in-depth learning and construction (7)-MyBatis delayed loading

1. What is query cache

Mybatis provides query caching to reduce database pressure and improve database performance.

Mybatis provides first-level cache and second-level cache.

The first-level cache is a SqlSession-level cache. When operating the database, you need to construct a sqlSession object. There is a data structure (HashMap) in the object for storing cache data. The cache data areas (HashMap) between different sqlSession do not affect each other.

The second-level cache is a mapper-level cache. Multiple sqlSession operates the same Mapper's sql statement. Multiple sqlSession can share the second-level cache. The second-level cache is across sqlSession.

Why use caching?

If there is data in the cache, there is no need to obtain it from the database, which greatly improves system performance.

2. Level 1 Cache

2.1 Working Principle of Level 1 Cache

## The first time the query is initiated for user information with user ID 1 , first find out whether there is user information with id 1 in the cache. If not, query the user information from the database.

Get the user information and store the user information in the first-level cache.

If sqlSession performs a commit operation (performing insert, update, delete), clear the first-level cache in sqlSession. The purpose of this is to store the latest information in the cache and avoid dirty reads.

The second time it is sent to query the user information with user ID 1, first find out whether there is user information with ID 1 in the cache. If there is in the cache, get the user information directly from the cache.

2.2 First-level cache test

Mybatis supports first-level cache by default and does not need to be configured in the configuration file.

Follow the first-level cache principle steps above to test.

    @Testpublic void testCache1() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);//下边查询使用一个SqlSession//第一次发起请求,查询id为1的用户User user1=userMapper.findUserById(1);
        System.out.println(user1);        //如果sqlSession去执行commit操作(执行插入、更新、删除),清空sqlSession中的一级缓存,//这样做的目的是为了让缓存中存储的是最新的信息,避免脏读。//更新user1的信息user1.setUsername("测试用户22");
        userMapper.updateUser(user1);//执行commit操作去清空缓存        sqlSession.commit();        //第二次发起请求,查询id为1的用户User user2=userMapper.findUserById(1);
        System.out.println(user2);

        sqlSession.close();
    }
2.3 Level 1 Cache Application

The official development is to integrate mybatis and spring, and the transaction is controlled in the service.

A service method includes many Mapper method calls.

service{

//When execution starts, start the transaction and create the SqlSession object

//The first time the mapper method is called findUserById(1)

//Call the mapper method findUserById(1) for the second time to get data from the first-level cache

//The method ends and sqlSession is closed

}

If two service calls are executed to query the same user information, the first-level cache will not be used. Because the session method ends, sqlSession will be closed and the first-level cache will be cleared.

3. Second-level cache

3.1 Second-level cache principle

First enable the second-level cache of mybatis.

sqlSession1 queries the user information with user ID 1. After querying the user information, it regrets that the query data is stored in the secondary cache.

If sqlSession3 executes SQL under the same mapper and executes commit submission, the data in the second-level cache area under the mapper will be cleared.

sqlSession2 queries the user information with user ID 1, checks whether data exists in the cache, and if so, directly retrieves the data from the cache.

The difference between the second-level cache and the first-level cache: the scope of the second-level cache is larger, and multiple sqlSession can share a UserMapper's second-level cache area. UserMapper has a second-level cache area (divided by namespace), and other mappers also have their own second-level cache areas (divided by namespace).

Each namespace mapper has a second-level cache area. If the namespaces of two mappers are the same, the data obtained by the two mappers executing SQL queries will be stored in the same second-level cache area.

3.2 Turn on the second-level cache

Mybatis’s second-level cache is mapper scope level. In addition to setting the main switch of the second-level cache in SqlMapConfig.xml, it must also be turned on in the specific mapper.xml. Level 2 cache.

Add to the core configuration file SqlMapConfig.xml:

<setting name="cacheEnabled" value="true"/>
##cacheEnabledConfigure global on/off settings for all caches under this configuration file. true falsetrue

在UserMapper.xml中开启二级缓存,UserMapper.xml下的sql执行完成后存储在它的缓存区域(HashMap)。

3.3调用pojo类实现序列化接口

为了将缓存数据取出执行反序列划操作,因为二级缓存数据存储介质多种多样,不一定在内存。可能在硬盘、远程等。

3.4测试方法

    @Testpublic void testCache2() throws Exception{
        SqlSession sqlSession1=sqlSessionFactory.openSession();
        SqlSession sqlSession2=sqlSessionFactory.openSession();
        SqlSession sqlSession3=sqlSessionFactory.openSession();
        
        UserMapper userMapper1=sqlSession1.getMapper(UserMapper.class);
        UserMapper userMapper2=sqlSession2.getMapper(UserMapper.class);
        UserMapper userMapper3=sqlSession3.getMapper(UserMapper.class);        //第一次发起请求,查询id为1的用户User user1=userMapper1.findUserById(1);
        System.out.println(user1);//这里执行关闭操作,将sqlSession中的数据写到二级缓存区域        sqlSession1.close();        //使用sqlSession3执行commit()操作User user=userMapper3.findUserById(1);
        user1.setUsername("Joanna");
        userMapper3.updateUser(user);//执行提交,清空UserMapper下边的二级缓存        sqlSession3.commit();
        sqlSession3.close();        //第二次发起请求,查询id为1的用户User user2=userMapper2.findUserById(1);
        System.out.println(user2);
        sqlSession2.close();
    }

3.5禁用二级缓存

在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql,默认情况是true,即该sql使用二级缓存。

Description

Allowed values

Default value

The above is the detailed content of What is query cache? Introduction to the usage of MyBatis query cache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn