Mybatis caching mechanism

MyBatis includes a very powerful query cache feature, which can be configured and customized very conveniently. Caching can greatly improve query efficiency.

mybatis实行sql的顺序: 查找二级缓存->一级缓存->数据库

● Two-level cache is defined by default in MyBatis system.

● Level 1 cache and Level 2 cache.

1. By default, only the first level cache (SqlSession level cache, also known as local cache) is enabled.
2. The second level cache needs to be manually opened and configured. It is a namespace-based cache.
3. In order to improve scalability. MyBatis defines the cache interface Cache. We can customize the secondary cache by implementing the Cache interface

1. Level 1 cache

The first level cache (local cache), that is, the local cache, the default scope is sqlSession. After Session flush or close, all Cache in this Session will be cleared. The local cache cannot be closed, but you can call clearCache (clear the local cache, or change the scope of the cache. After mybatis3.1, you can configure the scope of the local cache. Configure in mybatis.xml

· ·
localCacheScope MyBatis uses Local Cache to prevent circular references and speed up repeated nested queries. The default value is SESSION, in which case all queries executed in a session will be cached. If the value is STATEMENT, the local session is only used for statement execution, and different calls to the same SqlSession will not share data.

2. Level 1 cache demo & invalidation situation
as long as the data queried during the same session will be saved in a Map of the current SqlSession

● key: hashCode+ queried SqlId + written SQL query statement + parameters

● Four cases of the first level cache failure

1. Different SqlSessions correspond to different first-level caches.
2. The same SqlSession but with different query conditions.
3. Any addition, deletion or modification operation was performed
during two queries of the same SqlSession. 4. The cache was manually emptied during two queries of the same SqlSession.

3. Level 2 cache

  1. Second level cache (second level cache), global scope cache; the second level cache is not enabled by default and needs to be configured manually
  2. MyBatis provides the interface and implementation of the second level cache. The cache implementation requires POJO to implement the Serializable interface
  3. The second level cache will not take effect until the SqlSession is closed or submitted

Steps for usage

  1. Turn on the secondary cache in the global configuration file
  2. Use the cache configuration cache at the mapping file that needs to use the secondary cache
  3. Note: POJO needs to implement Serializable interface

4. Cache related attributes

<cache eviction="FIFO" flushInterval="60000" readOnly="false" size="1024"></cache>

1. eviction="FIFO": Cache recovery strategy: • The default is LRU.

● LRU-Least Recently Used: Remove objects that have not been used for the longest time.
● FIFO-first in first out: remove objects in the order they enter the cache.
● SOFT-soft reference: Remove objects based on garbage collector status and soft reference rules.
● WEAK-Weak references: More aggressively remove objects based on garbage collector status and weak reference rules.

2. flushInterval: refresh interval, in milliseconds
● The default is not set, that is, there is no refresh interval, the cache is only refreshed when the statement is called

3. size: the number of references, a positive integer
● Represents how many objects can be stored in the cache at most, too large may cause memory overflow

4. readOnly: read only, true/false
● true: read only cache; the same instance of the cache object will be returned to all callers. Therefore these objects cannot be modified. This provides a very important performance advantage.
● false: read-write cache; will return a copy of the cached object (through serialization). This is slower, but safe, so the default is false.

5. Cache related settings

1. The cacheEnable of the global setting:
● The switch for configuring the secondary cache. The first level cache is always open.

2. UseCache attribute of select tag:
● Configure whether this select uses secondary cache. Level 1 cache is always used

3. The flushCache attribute of the sql tag:
● Add, delete, change the default flushCache=true. After sql is executed, the first and second caches will be cleared at the same time.
The query default flushCache=false.

4. sqlSession.clearCache():
● Only used to clear the first level cache.

5. When a C/U/D operation is performed in a certain scope (first-level cache Session/second-level cache Namespaces), all caches in select under this scope will be cleared by default.

6. Third-party cache integration

● EhCache is a pure Java in-process caching framework, which is fast and capable, and is the default CacheProvider in Hibernate.
● MyBatis defines the Cache interface to facilitate our custom extensions.
● Steps:
  1. Import the ehcache package, and integrate the package, log package
   ehcache-core-2.6.8.jar、mybatis-ehcache-1.0.3.jar
   slf4j-api-1.6.1.jar、slf4j-log4j12-1.6.2.jar
  2. Write the ehcache.xml configuration file
  3. Configure the cache tag
  <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
● Refer to the cache: If you want to share the same cache configuration and instance in the namespace. You can use the cache-ref element to refer to another cache.


Thank you for reading. If you think reading this article is helpful to you, please click the "Like" button.

Guess you like

Origin blog.csdn.net/a251628111/article/details/107173363