Integrating ehcache caching framework in MyBatis advanced articles

Integrating ehcache caching framework in MyBatis advanced articles

 

I. Introduction

MyBatis provides us with the Cache interface and some implementation classes. Entering the source code of the Cache interface, you can see that the cache is a Map for MyBatis, which is relatively simple. But everyone knows that MyBatis is a framework that focuses on the persistence layer. MyBatis is very professional when dealing with databases, but it is slightly insufficient for caching. Even so, MyBatis provides a standard specification for the Cache interface. To achieve it, it provides good extensibility. In this section, we will introduce the integration of MyBatis with the Ehcache framework.

 

2. Preparation

Eclipse version: Luna Service Release 1 (4.4.1)
MyBatis version: org.mybatis.mybatis-3.2.8
JDK version: 1.7.0_79
Ehcache version: ehcache-core-2.6.11.jar
mybatis-ehcache integrated version: mybatis- ehcache-1.1.0.jar

 

3. Case

♠Refer to <<The Simple Introduction of MyBatis Basics>> to build a Maven project MyBatisIntegrateEhcache

♠After building the project, you need to import the ehcache related jar package into the project

import core package

<!-- ehcache核心jar包 -->
<dependency>
	<groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>

Introduce the mybatis-ehcache integration package. If you don't know how to import it, you can refer to the official documentation, as shown below:

Photos of MyBatis Advanced Integrating the ehcache Cache Framework - 1

Official address: http://www.mybatis.org/ehcache-cache/ If you are interested, you can take a look.

♠ Complete Pom file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.queen.mybatis</groupId> <artifactId>MyBatisIntegrateEhcache</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <!-- ehcache核心jar包 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency> <!-- MyBatis与ehcache整合jar包 --> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.1.0</version> </dependency> </dependencies> </project>

♠The configuration added below is configured according to the official documentation

Modify the EmpMapper.xml file and add the following configuration

Photos of MyBatis Advanced Integrating ehcache Cache Framework-3

Introduce the ehcache.xml file, and the complete configuration instructions are as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- java.io.tmpdir - Default temp file path 默认的 temp 文件目录 maxElementsInMemory:内存中最大缓存对象数. maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大. eternal:Element是否永久有效,一但设置了,timeout将不起作用. overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中 timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0, 也就是可闲置时间无穷大 timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用, 默认是0.也就是element存活时间无穷大. diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二) diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区. --> <diskStore path="E:\20170819\ehcache" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache> 

ehcache.xml placement path

Photos of MyBatis Advanced Integrating the ehcache Cache Framework - 5

As above, these are the steps we need to integrate ehcache. Let's test it briefly.

@Test
public void testEhCache() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmpMapper mapper = openSession.getMapper(EmpMapper.class); Emp emp01 = mapper.findEmpById(1); System.out.println(emp01.toString()); Emp emp02 = mapper.findEmpById(1); System.out.println(emp02.toString()); System.out.println(emp01 == emp02); } finally { openSession.close(); } }

The test results are as follows

2017-08-19 11:27:18,090 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==>  Preparing: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp where id = ? 
2017-08-19 11:27:18,144 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==> Parameters: 1(Integer)
2017-08-19 11:27:18,175 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] <==      Total: 1
Emp [id=1, empName=queen3aasd21, empEmail=[email protected], deptId=1] 2017-08-19 11:27:18,177 [main] [com.queen.mybatis.mapper.EmpMapper]-[DEBUG] Cache Hit Ratio [com.queen.mybatis.mapper.EmpMapper]: 0.0 Emp [id=1, empName=queen3aasd21, empEmail=[email protected], deptId=1] true 2017-08-19 11:27:18,178 [main] [net.sf.ehcache.store.disk.Segment]-[DEBUG] put added 0 on heap

The console prints a piece of SQL, and the second query gets it directly from the cache.

From the above print results, we can't seem to judge whether the ehcache cache is really used, but we can see if there is data in the cache path.

Photos of MyBatis Advanced Integrating the ehcache Cache Framework - 7

Find the path, you can be sure that the ehcache cache is used

Photos of MyBatis Advanced Integrating the ehcache Cache Framework - 9

 

4. Summary

The integration steps of MyBatis and Ehcache are summarized as follows:

  • Introduce third-party cache packages
  • Import the adaptation package integrated with the third-party cache
  • Introduce <cache type=”org.mybatis.caches.ehcache.EhcacheCache”/> in the mapper.xml mapping file
  • Introduce the ehcache.xml file and set the relevant attribute configuration

So far, we have completed the introduction of the integrated ehcache caching framework in the advanced chapter of MyBatis.

Blog address: http://www.marsitman.com/mybatis/mybatis-ehcache.html
Copyright statement: This article is an original article by the blogger, and reprinting is permitted, but the source must be indicated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324952440&siteId=291194637