Today to share with you MyBatis cache knowledge introduction, I hope to help you in the daily development!
When MyBatis opens a session with the database, MyBatis will automatically create a SqlSession object to represent the session of the database. In the same database session, MyBatis provides the optimization scenario of level 1 cache. For the same SQL query statement, it will preferentially hit level 1 cache to avoid database query again, thus improving query performance and reducing database pressure.
Enable level 1 cache MyBatis config file add the following:
Level-1 cache failure scenario SqlSeesion instance is different
SqlSeesion If the instance is the same, the query conditions are different
The SqlSeesion objects are the same and the query conditions are the same, but the operation is added, deleted, or modified between the two queries
The SqlSeesion object is the same, and the two query conditions are the same. No other operations are performed in the process, but the clearCache() method is used
Summary MyBatis level 1 cache lifecycle is consistent with SqlSession. It is enabled by default.
The performance of MyBatis level 1 cache using HashMap is poor
In a distributed environment, database operations may cause dirty data. Therefore, it is not recommended to enable MyBatis Level-1 cache
MyBatis level 1 cache life cycle is a SqlSession, if multiple SQLsessions need to share cache, then need to enable level 2 cache, after enabling level 2 cache, CachingExecutor is used to decorate Executor. Before entering level-1 cache query process, CachingExecutor is used to perform level-2 cache query.
2. Why is there a level 2 cache? 1. To avoid frequent interaction with the database. This is the main reason for designing caches.
2. When Spring and MyBatis integrate, sqlSession will be closed after each query, after which data will be emptied. So with the integration of MyBatis and Spring, level 1 caching is meaningless. If level-2 cache is enabled and SQLSession is disabled, the data in the Level-2 cache of the SQLSession is added to the Level-2 cache of the Mapper Namespace. In this way, the cache persists even after sqlSession is closed.
When level-2 cache is enabled, the sequence of database query is level-2 cache -> Level-1 cache -> database
Level 2 cache is not enabled by default, you need to manually enable level 2 cache, MyBatis configuration file added as follows:
Eviction: Set reclamation policy. The default is THE LRU policy.
LRU – Least recent collection, removes objects that have not been used for the longest time
FIFO – First in, first out, remove caches in the order they are entered
Soft-soft reference, which removes objects based on garbage collector status and SOFT reference rules
WEAK – WEAK references, which more aggressively removes objects based on garbage collector and WEAK reference rules
Flushinterval: specifies the interval at which the cache is flushed. The value is not flushed by default
ReadOnly: indicates whether it is read-only. True Read-only, MyBatis considers all operations that fetch data from the cache to be read-only and do not modify the data. To speed up the retrieval of data, MyBatis will directly give the user a reference to the data in the cache. It’s not safe. It’s fast. Read/write (default) : MyBatis feels that data may be modified
Size: How many elements can be stored in the cache
Type: specifies the full class name of the custom Cache.
Blocking: If the corresponding key cannot be found in the cache, does the blocking continue until the corresponding data enters the cache?
Note:
1. It is not actually stored in the second level cache before the transaction is committed, but a temporary property is stored in the second level cache after the transaction is committed. Therefore, a commit transaction is required to take effect.
2. If the MyBatis default cache is used, the result set object must implement Serializable interface, otherwise an error will be reported.
1. It is suitable for frequent access and users do not have high requirements on real-time query results.
In this case, level2 cache can reduce the database access and improve the database performance. For example, SQL that takes a long time to query statistical reports, SQL that queries by fixed time dimensions (monthly order information, etc.).
2, suitable for query more write less scenario open.
Any operation (INSERT, UPDATE, delete) to the database triggers an update to the cache, which invalidates the cache.
5. Level 2 cache failure Scenario The first SqlSession is not submitted
The (INSERT, UPDATE, DELETE) operations performed on the corresponding tables of the database
6. Conclusion MyBatis level 2 cache is not recommended for most business systems, because business systems have frequent data operations and their own level 2 cache performance is not very high. Level 2 caching is hardly useful. Third-party caches can be referenced.
IT technology sharing community
Personal blog website: Programmerblog.xyz