Home > Java > javaTutorial > body text

Detailed explanation of MyBatis query cache in java

怪我咯
Release: 2017-06-30 10:57:30
Original
1455 people have browsed it

The use of query cache is mainly to improve query access speed. This article mainly introduces the MyBatis query cache. Friends who need it can refer to the use of

, mainly to improve the query access speed. Simplify the user's repeated query process for the same data, instead of obtaining the result data from the database query every time, thereby improving access speed.

MyBatis's query caching mechanism can be divided into two types according to the scope of the cache area (life cycle): first-level cache and second-level cache

1. First-level query cache

MyBatis first-level cache is a HashMap local cache based on the org.apache.ibatis.cache.impl.PerpetualCache class, and its scope is Sqlsession. Execute the same SQL statement twice in the same Sqlsession. After the first execution, the query results will be written to the cache. The second time, the data will be obtained directly from the cache without querying the database, thereby improving Query efficiency.

When a Sqlsession ends, the first-level cache in the Sqlsession no longer exists. MyBatis's first-level cache is turned on by default and cannot be turned off.

1. Proof of existence of first-level cache

Test class:

//证明一级缓存的存在
@Test
public void test01(){
 //第一次查询
 Student student = dao.selectStudentById(2);
 System.out.println(student);
 //第二次查询
 Student student2 = dao.selectStudentById(2);
 System.out.println(student2);  
}
Copy after login

mapper:

<mapper namespace="com.hcx.dao.IStudentDao">  
  <select id=selectStudentById resultType="com.hcx.beans.Student">
   select * from student where id=#{id}
  </select>
</mapper>
Copy after login


Console:

After the execution, it was found that only one query from the DB was executed, and the second result was output directly. Note that the second time is read from the Sqlsession cache.

2. The basis for reading data from the cache is the sql id

The first-level cache caches the same sql mapping id The query results, rather than the query results of the same sql statement. Because MyBatis internal query cache, whether it is a first-level query or a second-level query, the bottom layer is implemented using a hashmap: the key is the id-related content of the SQL, and the value is the result of the query from the database.

mapper:


<mapper namespace="com.hcx.dao.IStudentDao">
  <select id=selectStudentById resultType="com.hcx.beans.Student">
   select * from student where id=#{id}
  </select>
  <select id="selectStudnetById2" resultType="com.hcx.beans.Student">
   select id,name,age,score,birthday from student where id=#{id}
  </select>
</mapper>
Copy after login

dao interface:

public interface IStudentDao {  
 Student selectStudentById(int id);
 Student selectStudentById2(int id); 
}
Copy after login

Test class:

//证明从一级缓存中读取数据的依据:
//MyBatis:sql的id+sql语句
//hibernate:查询结果对象的id
@Test
public void test02(){
 Student student = dao.selectStudentById(2);
 System.out.println(student);

 Student student2 = dao.selectStudentById2(2);
 System.out.println(student2);  
}
Copy after login

Console:

Looking at the console, we found that the results of the second query were exactly the same as the first, but the second query did not read data from the cache, but directly from the DB. This is because the basis for reading data from the cache is the mapping ID of the query sql, not the query result.

3. The impact of additions, deletions and modifications on the first-level query cache

Addition, deletion and modification operations, regardless of whether Sqlsession.commit( ), will clear the first-level query cache so that the query can be selected from the DB again.

Test class:

@Test
public void test03(){
 Student student = dao.selectStudentById(2);
 System.out.println(student);
 //增删改操作都会清空一级缓存,无论是否提交
 dao.insertStudent(new Student("赵六",26,96.6));
 Student student2 = dao.selectStudentById(2);
 System.out.println(student2);  
}
Copy after login

Console:


## 2. Built-in secondary query cache

The scope of MyBatis query cache is divided according to the namespace of the mapping file mapper. The mapper query data of the same namespace is stored in the same cache area. Data in different namespaces do not interfere with each other.

Both the first-level cache and the second-level cache are stored separately according to namespace. But the difference between the first and second level cache is that once the Sqlsession is closed, the data in the Sqlsession will not exist, that is, the first level cache will no longer exist. The life cycle of the second-level cache will be synchronized with the entire application, regardless of whether the Sqlsession is closed.

The purpose of using the second-level cache is not to share data, because MyBatis reads data from the cache based on the sql id, not the queried object. Therefore, the data in the second-level cache is not intended to be shared between multiple queries (as long as the object exists in the query results in all queries, it is read directly from the cache. This is the sharing of data. The cache in hibernate is For sharing, but MyBatis is not), but to extend the storage time of the query results and improve system performance.

1. Second level cache usage

Using the second level cache only requires two steps:

Serialized entities

Add the tag in the mapper mapping file

1. Entity serialization

Requires that the entity class involved in the query result must implement java.io .Serializable interface. If the entity class has a parent class, or it has domain attributes, the parent class and domain attribute class must also implement the

serialization interface.

public class Student implements Serializable{
 private Integer id;
 private String name;
 private int age;
 private double score;
}
Copy after login

2. Add the tag to the mapper mapping file

Add < to the tag in the mapper mapping file cache/>Sub-tag

<mapper namespace="com.hcx.dao.IStudentDao">
 <cache/>
  <select id=selectStudentById resultType="com.hcx.beans.Student">
   select * from student where id=#{id}
  </select>
</mapper>
Copy after login

3. Second-level cache configuration

Add some related attribute settings to the tag, you can set the second-level cache Control cache performance. If no settings are specified, the default values ​​are maintained.

<cache eviction="IFIO" flushInterval="10800000"
  readOnly="true" size="512"/>
Copy after login

eviction:逐出策略。当二级缓存中的对象达到最大值时,就需要通过逐出策略将缓存中的对象移出缓存。默认为LRU。常用的策略有FIFO和LRU

flushInterval:刷新缓存的时间间隔,单位毫秒。这里的刷新缓存即清空缓存。一般不指定,即当执行增删改时刷新缓存。

readOnly:设置缓存中数据是否只读。只读的缓存会给所有调用者返回缓存对象的相同实例,因此这些对象不能被修改,这提供了很重要的性能优势。但读写的缓存会返回缓存对象的拷贝。这会慢一些,但是安全,因此默认是false。
size:二级缓存中可以存放的最多对象个数。默认为1024个。

2.二级缓存的存在性证明

对于映射文件中的同一个查询,肯定是同一个namespace中的查询。在一次查询后,将Sqlsession关闭,再进行一次相同查询,发现并没有到DB中进行select查询,说明二级缓存是存在的。

//证明二级缓存的存在
@Test
public void test01(){
 //第一次查询
 Student student = dao.selectStudentById(2);
 System.out.println(student);
 sqlSession.close();
 sqlSession = MyBatisUtils.getSqlSession();
 dao = sqlSession.getMapper(IStudentDao.class);
 //第二次查询
 Student student2 = dao.selectStudentById(2);
 System.out.println(student2);  
}
Copy after login


查看控制台:

Cache Hit Ratio表示缓存命中率。开启二级缓存后,每执行一次查询,系统都会计算一次二级缓存的命中率。第一次查询也是先从缓存中查询,只不过缓存中一定是没有的。所以会再从DB中查询。由于二级缓存中不存在该数据,所以命中率为0.但第二次查询是从二级缓存中读取的,所以这一次的命中率为1/2=0.5。当然,若有第三次查询,则命中率为1/3=0.66

3.增删改对二级缓存的影响

增删改操作,无论是否进行提交sqlSession.commit(),均会清空一级、二级缓存,使查询再次从DB中select。

测试类:

@Testpublic void test02(){
 //第一次查询
 Student student = dao.selectStudentById(2);
 System.out.println(student);
 sqlSession.close();
 sqlSession = MyBatisUtils.getSqlSession();
 dao = sqlSession.getMapper(IStudentDao.class);
 //插入
 dao.insertStudent(new Student("",0,0));
 //第二次查询
 Student student2 = dao.selectStudentById(2);
 System.out.println(student2);  
}
Copy after login

控制台:

注意,在第二次查询时的缓存命中率为0.5,但还是从DB中查询了。说明在缓存中与该查询相对应的key是存在的,但其value被清空。而value被清空的原因是前面执行了对DB的增删改操作,所以不会从缓存中直接将null值返回,而是从DB中进行查询。

说明:

二级缓存的清空,实质上是对所查找key对应的value置为null,而非将对,即entry对象删除。

从DB中进行select查询的条件是:缓存中根本不存在这个key或者缓存中存在该key所对应的entry对象,但value为null。

设置增删改操作不刷新二级缓存:

若要使某个增、删或改操作不清空二级缓存,则需要在其中添加属性flushCache="false",默认为true。

<insert id="insertStudent" flushCache="false">
  insert into student(name,age,score) values(#{name},#{age},#{score})
 </insert>
Copy after login

4.二级缓存的关闭

二级缓存默认为开启状态。若要将其关闭,则需要进行相关设置。
根据关闭的范围大小,可以分为全局关闭和局部关闭

1.全局关闭(在配置文件中设置)

全局关闭是指整个应用的二级缓存全部关闭,所有查询均不使用二级缓存。全局开关设置在主配置文件的全局设置中,该属性为cacheEnabled,设置为false,则关闭;设置为true,则开启,默认值为true。即二级缓存默认是开启的。

<!-- 关闭二级缓存 -->
<settings>
 <setting name="cacheEnabled" value="false"/>
</settings>
Copy after login

2.局部关闭(在映射文件的每个select中设置)

局部关闭指整个应用的二级缓存是开启的,但只是针对某个标签的二级缓存。

在该要关闭二级缓存的查询的二级缓存默认是开启的。

<!--useCache="false"对当前sql的二级缓存的局部关闭 -->
 <select id=selectStudentById useCache="false" resultType="com.hcx.beans.Student">
  select * from student where id=#{id}
 </select>
Copy after login

5.二级缓存的使用原则

1.只能在一个命名空间下使用二级缓存

由于二级缓存中的数据是基于namespace的,即不同namespace中的数据互不干扰。在多个namespace中若均存在对同一个表的操作,那么这多个namespace中的数据可能就会出现不一致现象。

2.在单表上使用二级缓存

如果一个表与其它表有关联关系,那么久非常有可能存在多个namespace对同一数据的操作。而不同namespace中的数据互补干扰,所以就有可能出现多个namespace中的数据不一致现象。

3.查询多于修改时使用二级缓存

在查询操作远远多于增删改操作的情况下可以使用二级缓存。因为任何增删改操作都将刷新二级缓存,对二级缓存的频繁刷新将降低系统性能。

三、ehcache二级查询缓存

MyBatis允许使用第三方缓存产品。ehCache就是其中一种。

注意:使用ehcache二级缓存,实体类无需实现序列化接口。

1.导入jar包

2.添加ehcache.xml

解压ehcache的核心jar包ehcache-core-2.6.8.jar,将其中的一个配置文件ehcache-failsafe.xml直接放到项目的src目录下,并更名为ehcache.xml

(1)标签

指定一个文件目录,当内存空间不够,需要将二级缓存中数据写到硬盘上时,会写到这个指定目录中。其值一般为java.io.tmpdir,表示当前系统的默认文件临时目录。

当前文件系统的默认文件临时目录,可以通过System.property()方法查看:

@Test
public void test(){
 String path = System.getProperty("java.io.tmpdir");
 System.out.println(path);
}
Copy after login

(2)标签

3.启用ehcache缓存机制

在映射文件的mapper中的中通过type指定缓存机制为Ehcache缓存。默认为MyBatis内置的二级缓存org.apache.ibatis.cache.impl.PerpetualCache。

<mapper namespace="com.hcx.dao.IStudentDao">
 <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
  <select id=selectStudentById resultType="com.hcx.beans.Student">
   select * from student where id=#{id}
  </select>
</mapper>
Copy after login

4.ehcache在不同mapper中的个性化设置

在ehcache.xml中设置的属性值,会对该项目中所有使用ehcache缓存机制的缓存区域起作用。一个项目中可以有多个mapper,不同的mapper有不同的缓存区域。对于不同缓存区域也可进行专门针对于当前区域的个性设置,可通过指定不同mapper的属性值来设置。

属性值的优先级高于ehcache.xml中的属性值。

<mapper namespace="com.hcx.dao.IStudentDao">
 <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
   <property name="maxElementsInMemory" value="5000"/>
   <property name="timeToIdleSeconds" value="240"/>
 </cache>
</mapper>
Copy after login

The above is the detailed content of Detailed explanation of MyBatis query cache in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template