Mybatis learning tutorial (VII) – Introduction to mybatis cache

1、 Introduction to mybatis cache

Like most persistence layer frameworks, mybatis also provides L1 and L2 cache support

1. L1 cache:

The storage scope of HashMap local cache based on perpetualcache is session. After session flush or close, all caches in the session will be emptied.  

2. L2 cache:

The second level cache has the same mechanism as the first level cache. By default, it is stored in perpetual cache and HashMap. The difference is that its storage scope is mapper (namespace) and the storage source can be customized, such as ehcache.

3. For the cache data update mechanism, when a C / U / D operation is performed on the of a scope (L1 cache session / L2 cache namespaces), by default, the caches in all selections under the scope will be cleared.

1.1. Mybatis L1 cache test

1.2 mybatis L2 cache test

1. Open the L2 cache in usermapper Add the following configuration to the XML file

2. Test L2 cache

1.3 supplementary description of L2 cache

  1. All select statements in the mapping statement file will be cached.

  2. All insert, update and delete statements in the mapping statement file refresh the cache.

  3. The cache will be reclaimed using the least recently used (LRU) algorithm.

  4. The cache is refreshed according to the specified time interval.

  5. The cache stores 1024 objects

Common attributes of cache tag:

Add some knowledge to you:

Like hibernate, mybatis has a caching mechanism

The L1 cache is a HashMap local cache based on the perpetualcache (built-in by mybatis), and its scope is session. Therefore, when the session is committed or closed, the cache will be emptied

The L2 cache is also based on the perpetual cache by default, but the storage source can be specified for it, such as ehcache

The first level cache caches SQL statements, while the second level cache caches result objects. See the following example (the log level of mybatis is set to debug)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>