SoFunction
Updated on 2025-04-06

MyBatis's first-level and second-level cache and advantages description

Level 1 cache

MyBatis' local cache means that when the same query statement is executed in the same SqlSession, the query results will be cached in memory. The next time the same query statement is executed, the result will be directly obtained from the cache without accessing the database again.

The default enablement of local cache is set in SqlSessionFactory. You can turn off local cache by adding useCache="false" to the select tag in the file.

Level 2 cache

MyBatis's secondary cache means that when the same query statement is executed in multiple SqlSessions, the query results will be cached in memory. The next time the same query statement is executed, the result will be directly obtained from the cache without accessing the database again.

Unlike local cache, the secondary cache is cross-SqlSession, that is, the cache results for a query statement in a SqlSession can be shared by other SqlSession.

The default enablement of the secondary cache is also set in SqlSessionFactory. You can enable the secondary cache by adding type="" to the cache tag in the file.

At the same time, it should be noted that the implementation of level 2 cache needs to meet the following two conditions:

  • You must add the cache tag to the select tag in the file and specify the cache id.
  • The return value type of the query statement must be serializable because the cache is stored in memory and the cache results need to be serialized to disk. If the return value type is not serializable, an exception will be thrown when cached.

Advantages of Level 2 Caching

1. Reduce the number of database accesses

Using a secondary cache can reduce the number of accesses to the database, thereby increasing the response speed of your application.

When an application needs to repeatedly query the same data, it can directly get the data from the cache without accessing the database again.

2. Improve application performance

Using a secondary cache can cache query results into the application's memory, accessing memory is much faster than accessing databases.

This can greatly improve the performance of the application, especially in high concurrency.

3. Reduce the database load

Using secondary cache can reduce the load on the database and reduce the pressure on the database.

When an application needs to query the same data, it can directly obtain the data from the cache without accessing the database again, which can reduce the load on the database.

4. Improve the scalability of the application

Using Level 2 cache can improve the scalability of your application. When an application needs to be extended, caches can be shared between multiple servers, thus avoiding the problem of data inconsistency and improving the scalability of the application.

In short, using L2 cache can significantly improve the performance and scalability of your application, reduce the number of accesses and loads of the database, and is a very useful feature.

However, it should be noted that the secondary cache may cause data inconsistency and needs to be configured and used according to the specific situation.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.