1. Introduction to MyBatis cache
Just like most persistence layer frameworks, MyBatis also provides support for L1 and L2 caches.
1. Level 1 cache:
The HashMap local cache based on PerpetualCache has a storage scope of Session. After Session flush or close, all caches in the Session will be cleared.
2. Level 2 cache:
The second-level cache is the same as the first-level cache, and the default is PerpetualCache and HashMap storage. The difference is that its storage scope is Mapper (Namespace), and it can customize the storage source, such as Ehcache.
3. For the cache data update mechanism , when a certain scope (first-level cache Session/second-level cache Namespaces) is performed, all caches in selects under this scope will be cleared by default.
1.1. Mybatis Level 1 Cache Test
package me.gacl.test;import me.gacl.domain.User;import me.gacl.util.MyBatisUtil;import org.apache.ibatis.session.SqlSession;import org.junit.Test;/*** @author gacl* Test first-level cache*/public class TestOneLevelCache {/** Level 1 cache: Session-level cache (on by default)*/@Testpublic void testCache() {SqlSession session = MyBatisUtil.getSqlSession();String statement = "me.gacl.mapping.userMapper.getUser";User user = session.selectOne(statement, );System.out.println(user);/** First-level cache will be used by default */user = session.selectOne(statement, );System.out.println(user);session.close();/*. It must be the same Session. If the session object has been closed(), it is impossible to use it */session = MyBatisUtil.getSqlSession();user = session.selectOne(statement, );System.out.println(user);/*. The query conditions are the same */user = session.selectOne(statement, );System.out.println(user);/*. No session.clearCache() has been executed to clean the cache*///session.clearCache(); user = session.selectOne(statement, );System.out.println(user);/*. No add/delete operations have been executed (these operations will clean the cache)*/session.update("me.gacl.mapping.userMapper.updateUser",new User(, "user", ));user = session.selectOne(statement, );System.out.println(user);}} 1.2. Mybatis Level 2 Cache Test
1. Turn on the secondary cache and add the following configuration to the userMapper.xml file
<mapper namespace="me.gacl.mapping.userMapper"><!-- Turn on Level 2 Cache--><cache/>
2. Test the secondary cache
package me.gacl.test;import me.gacl.domain.User;import me.gacl.util.MyBatisUtil;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.junit.Test;/*** @author gacl* Test Level 2 cache*/public class TestTwoLevelCache {/** Test Level 2 cache* Use two different SqlSession objects to execute queries with the same query conditions. SQL statements will not be sent during the second query, but data is directly fetched from the cache*/@Testpublic void testCache() {String statement = "me.gacl.mapping.userMapper.getUser";SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();//Open two different SqlSessionSqlSession session = factory.openSession();SqlSession session = factory.openSession();//When using secondary cache, the User class must implement a Serializable interface===> User implements SerializableUser user = session.selectOne(statement, );session.commit();//I don't understand why, this place must be submitted after the transaction is submitted before the second level cache will work System.out.println("user="+user);//Because two different SqlSession objects are used, even if the query conditions are the same, the first level cache will not be enabled. user = session.selectOne(statement, );//session.commit();System.out.println("user="+user);}} 1.3. Supplementary instructions for secondary cache
1. All select statements in the mapping statement file will be cached.
2. Map all insert, update and delete statements in the statement file will refresh the cache.
3. The cache will be retried using the Least Recently Used (LRU, the least recently used) algorithm.
4. The cache will be refreshed according to the specified time interval.
5. The cache will store 1024 objects
Common properties of cache tags:
<cache eviction="FIFO" <!--Recycling strategy is first-in, first-out -->flushInterval="60000" <!--Automatic refresh time 60s-->size="512" <!--Cache up to 512 reference objects -->readOnly="true"/> <!--Read-only-->
I'll give you some knowledge:
Like hibernate, mybatis also has a cache mechanism
Level 1 cache is based on the HashMap local cache of PerpetualCache (mybatis comes with) and its scope is session, so when the session commit or close, the cache will be cleared.
The second-level cache is also based on PerpetualCache by default, but it can be used to create storage sources, such as ehcache.
The first-level cache caches SQL statements, while the second-level cache caches the result object. See the following example (mybatis' log level is set to debug)