Home > Java > javaTutorial > Detailed explanation of MyBatis code examples on second-level cache issues

Detailed explanation of MyBatis code examples on second-level cache issues

黄舟
Release: 2017-03-24 10:34:36
Original
1792 people have browsed it

This article mainly introduces MyBatis's issue with the second-level cache. The second-level cache is a Mapper-level cache. Multiple sqlSession operates the same Mapper, and its second-level cache It can be shared.

MyBatis provides first-level cache and second-level cache. The first-level cache is a sqlSession-level cache. The caches between different sqlSession do not affect each other. The second-level cache is a Mapper-level cache. Multiple sqlSession operates on the same Mapper, and its second-level cache can be shared.

MyBatis has a variety of second-level cache solutions to choose from. Among them, the support for Memcached is relatively mature. We will take Memcached as an example to introduce the integration with the spring project.

Use configuration

Configure pom.xml and add dependencies.

<dependencies>
 ...
 <dependency>
  <groupId>org.mybatis.caches</groupId>
  <artifactId>mybatis-memcached</artifactId>
  <version>1.0.0</version>
 </dependency>
 ...
</dependencies>
Copy after login

Global switch

<settings>
  <setting name="cacheEnabled" value="true"/>
</settings>
Copy after login

Configure the mapper.xml that needs to be cached

The second-level cache is Mapper level, you can Enable second-level cache for specific mapper.xml.

<mapper namespace="org.acme.FooMapper">
 <cache type="org.mybatis.caches.memcached.MemcachedCache" />
</mapper>
Copy after login

After it is turned on, all select statements in the mapper will be cached by default. All insert, update, and delete statements under this namespace will cause the cache in the space to be cleared.

Cache can be disabled for a specific statement.

 <select id="retObj" resultMap="results" useCache="false" >
   select id, name, sex from employee  
 </select>
Copy after login

You can also trigger a refresh action for a certain statement (default corresponds to all insert, update, delete statements).

 <select id="retObj" resultMap="results" flushCache="true" >
   select id, name, sex from employee  
 </select>
Copy after login

Configuration parameters are set in memcached.properties. If missing, the default settings are used.

The following are the configuration parameters:

If you need to record the cache operation log, you can achieve it by the following configuration

<mapper namespace="org.acme.FooMapper">
 <cache type="org.mybatis.caches.memcached.LoggingMemcachedCache" />
 ...
</mapper>
Copy after login

Notes

Classes cached in Memcached need to implement the Serializable interface, otherwise the error java.io.NotSerializableException will be reported.

Second-level cache application scenarios

  1. It is recommended to use the second-level cache for data with high query frequency and low change frequency.

  2. For query requests that access a lot and users do not have high requirements for real-time query results, mybatis second-level caching technology can be used to reduce database access and improve access speed. Business scenarios such as : Time-consuming statistical analysis SQL, telephone bill query SQL, etc.

The implementation method is as follows:

By setting the refresh interval, mybatis automatically clears the cache every period of time, according to data changes Frequency setting cache refresh interval flushInterval, for example, set to 30 minutes, 60 minutes, 24 hours, etc., depending on needs.

mybatis limitations

Mybatis second-level cache is not good at fine-grained data level caching.

For example, the following requirements: cache product information. Due to the large number of product information query visits, users are required to query the latest product information every time. At this time, if you use the second-level cache of mybatis It is impossible to achieve that when a product changes, only the cache information of the product will be refreshed without refreshing the information of other products, because the second-level cache area of ​​​​mybaits is divided in units of mappers. When the information of a product changes, all products will be refreshed. All cached data of the message are cleared. Solving such problems requires targeted caching of data based on needs at the business layer.

The above is the detailed content of Detailed explanation of MyBatis code examples on second-level cache issues. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template