4

A programmer who has worked for 5 years keeps complaining to me in private messages.

He said that he has been using Mybatis for so long, so he is proficient in Mybatis.

The result turned out to be overturned on the interview question of Mybatis! Really annoying!

Well, let's take a look at the "cache mechanism in Mybatis" today, the answers of ordinary people and experts.

Ordinary people:

Ok. . . . . . . . .

Expert:

This question is a bit complicated, and I intend to explain it from several aspects.

First of all, Mybatis has designed a secondary cache to improve the efficiency of data retrieval, avoiding the need to query the database every time the data is accessed.

image-20220411223435810

The first-level cache is the SqlSession-level cache, also called the local cache, because each user needs to use SqlSession to execute the query.

In order to avoid checking the database every time, Mybatis saves the queried data in the local cache of SqlSession. If the subsequent SQL hits the cache, it can be read directly from the local cache.

If you want to implement caching across the SqlSession level? Then the first-level cache cannot be realized, so the second-level cache is introduced in Mybatis, that is, when multiple users

When querying data, only any one SqlSession gets the data and puts it into the second-level cache, and other SqlSessions can load data from the second-level cache.

The specific implementation principle of each L1 cache is as follows:

image-20220411224117473

An Executor is held in the SqlSession, and each Executor has a LocalCache object.

When a user initiates a query, Mybatis will query in the Local Cache according to the execution statement. If there is no hit, it will query the database and write it to the Local Cache, otherwise it will return directly.

Therefore, the life cycle of the cache is SqlSession, and in multiple Sqlsessions or distributed environments, it may lead to dirty data in database write operations.

The specific implementation principle of the second level cache is as follows:

image-20220411224448255

The Executor is decorated with CachingExecutor, so before entering the query process of the first-level cache, the second-level cache query will be performed through the CachingExecutor.

After the second level cache is turned on, it will be shared by multiple SqlSessions, so it is a global cache. Therefore, its query process is to first check the second-level cache, then the first-level cache, and finally the database.

In addition, compared with the first-level cache, the second-level cache of MyBatis realizes the sharing of cached data between SqlSessions, and the cache granularity can also reach the namespace level. Control is also stronger.

The above is my understanding of the problem.

Summarize

In actual business scenarios, the design idea of multi-level cache is very worth learning and learning.

So I think this interview question is pretty good.

Everyone should keep in mind that the purpose of learning the underlying technology is to improve the technical thinking ability and accumulate solutions, paving the way for higher positions in the future.

This issue of ordinary people VS master interview series ends here.

If you have any technical interview questions that you don't understand, please feel free to private message me at any time.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。