Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement MybatisPlus second-level cache

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to achieve MybatisPlus secondary cache, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to achieve MybatisPlus secondary cache. Let's take a look.

I. Preface

This paper continues [the exploration of Mybatis cache system] and provides a two-tier cache solution based on MybatisPlus technology that can be used in production environment.

1. Pre-conditions

Master the principle of MyBatis secondary cache

For more information about the principle of MyBatis caching, please see [Mybatis Cache system].

Select a qualified version of MybatisPlus

This scheme has strict restrictions on the version of MybatisPlus, and the version used is not less than 3.4.3.4. If it is lower than this version, it will not be able to meet the closed-loop requirements in the production environment.

Correctly handle join (multi-table) queries

For the correct multi-table join query, please refer to [MybatisPlus join query solution]

2. Goal and harvest

If the application has distributed cache requirements, then directly discard the second-level cache solution and directly choose the business layer cache scheme.

If the application does not have a distributed scenario, then through a simple transformation, the introduction of two-level cache can greatly improve the response efficiency.

Second, principle analysis 1. Secondary cache

The biggest consideration of choosing MybatisPlus to achieve secondary cache is the single table operation it uses, in other words, the premise of correct use of secondary cache is that the traditional multi-table join operation can not be used, otherwise there must be a situation that the cached data can not be updated in real time.

2. Cache data updates

All cached data must be related to data update, and secondary cache also needs to actively update data. Secondary caching is in namespaces, in other words, data updates within the same namespace will automatically trigger cache updates (essentially data invalidation). The query operation caches the data, and adds, modifies and deletes the data to invalidate the cache.

The self-organized cache update and management not only improves the application response speed, but also reduces the complexity of cache management, which is helpful to improve the development efficiency.

3. The difference between caching

The second-level cache mentioned here needs to be distinguished from the third-level cache (business cache). The second-level cache refers to the DAO layer cache, and the purpose of using the cache is to reduce the impact of network IO on the application; the third-level cache refers to the business layer cache, mainly to reduce the occupation of CPU performance by complex computing.

III. Local secondary cache

For ordinary projects, the use of built-in local secondary cache can meet the needs. Here we take MybatisPlus as an example to illustrate how to use secondary cache correctly.

1. Two sets of API

MybatisPlus encapsulates two sets of API to access the database, one is BaseMapper-based API, the other is AR-based API, the two are not common in the use of secondary cache. It takes the form of data cached by BaseMapper's built-in API, which cannot be clearly cached when updating data using AR's built-in interface, at least not supported until this version.

Considering that the BaseMapper interface system is relatively rich, we choose to retain the BaseMapper interface and disable the AR interface, which ensures that a set of interfaces are used, so that the secondary cache can be refreshed in real time by calling add, delete, query and modify.

It is important to indicate that the Mapper layer and the Service layer use the same set of interfaces, so you can safely use them.

2. Cache implementation type

The default secondary cache implementation type is PerpetualCache, where the cache requires the cached object to implement the serialization interface. Other types of local caches are EhCache, Caffeine, and so on.

This is the end of the article on "how to implement MybatisPlus secondary cache". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve MybatisPlus secondary cache". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report