1、 Mybatis+Ehcache配置java
爲了提升MyBatis的性能,有時候咱們須要加入緩存支持,目前用的比較多的緩存莫過於ehcache緩存了,ehcache性能強大,並且位各類應用都提供瞭解決方案,在此咱們主要是作查詢緩存,提升查詢的效率.mysql
整合MyBatis和ehcache須要的jar包以下:算法
ehcache-core-2.4.4.jarspring
mybatis-ehcache-1.0.0.jarsql
slf4j-api-1.6.1.jar數據庫
slf4j-log4j12-1.6.2.jarapi
資源已上傳到百度網盤點擊此處下載,其中包括了一些mybatis的jar包,log4j,mysql驅動等必須的包緩存
將上述包加入項目以後,新建一個文件名,該文件名必須爲ehcache.xml,放在類路徑下面,內容以下:mybatis
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <!-- 緩存位置能夠是自定義的硬盤地址也能夠是JVM默認使用的緩存地址--> <!--<diskStore path="d:\cache"/> --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="true"/> <!-- 配置自定義緩存 name:Cache的惟一標識 maxElementsInMemory:緩存中容許建立的最大對象數 maxElementsOnDisk:磁盤中最大緩存對象數,如果0表示無窮大 eternal:Element是否永久有效,一但設置了,timeout將不起做用,對象永不過時。 timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡以前, 兩次訪問時間的最大時間間隔值,這隻能在元素不是永久駐留時有效, 若是該值是 0 就意味着元素能夠停頓無窮長的時間。 timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值, 這隻能在元素不是永久駐留時有效,若是該值是0就意味着元素能夠停頓無窮長的時間。 overflowToDisk:內存不足時,是否啓用磁盤緩存。 diskPersistent:是否緩存虛擬機重啓期數據 diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒 diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每一個Cache都應該有本身的一個緩衝區 memoryStoreEvictionPolicy:緩存滿了以後的淘汰算法。默認策略是LRU(最近最少使用)。你能夠設置爲FIFO(先進先出)或是LFU(較少使用) <cache name="SimplePageCachingFilter" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" memoryStoreEvictionPolicy="LFU" /> --> </ehcache>
該文件是ehcache的配置文件,上面的註釋已經說得很清楚了,這裏我用的是默認的配置app
至此ehcache已經配置好了,而後只須要在你想要緩存的mapper配置文件裏面加入如下內容,該查詢語句獲得的結果將會被緩存
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qiuqiu.dao.PersonDao"> <!-- 如下兩個<cache>標籤二選一,第一個能夠輸出日誌,第二個不輸出日誌 只要在對應的mapper配置文件中加入<cache />標籤便可--> <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> <!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> --> <select id="selectUserById" parameterType="int" resultType="org.qiuqiu.vo.Person"> select * from person where id=#{id} </select> </mapper>
這樣就對這個mapper裏面的各類結果進行了緩存。程序中不須要修改任何地方。
這個過程不復雜,也沒什麼難度,不過Mybatis的官方說的也太含糊了。附件下面有,須要的各類jar包已經包含。點擊下載附件。原文地址:http://qiuqiu0034.iteye.com/blog/1162952
2、 springMVC+mybatis+ehcache詳細配置
首先須要先須要兩個主要的jar包
ehcache-core-2.4.6.jar
mybatis-ehcache-1.0.1.jar
ehcache-core必定要1.3以上的版本 由於1.3以前好像不支持集羣的。而後須要建立一個ehcache.xml在類路徑下面
<?xml version="1.0" encoding="utf-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/> <!-- 配置自定義緩存 maxElementsInMemory:緩存中容許建立的最大對象數 eternal:緩存中對象是否爲永久的,若是是,超時設置將被忽略,對象從不過時。 timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡以前, 兩次訪問時間的最大時間間隔值,這隻能在元素不是永久駐留時有效, 若是該值是 0 就意味着元素能夠停頓無窮長的時間。 timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值, 這隻能在元素不是永久駐留時有效,若是該值是0就意味着元素能夠停頓無窮長的時間。 overflowToDisk:內存不足時,是否啓用磁盤緩存。 memoryStoreEvictionPolicy:緩存滿了以後的淘汰算法。 --> <cache name="testCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
上面的diskStor path 你能夠指定某一個路徑下,java.io.tmpdir 指的是你係統的緩存目錄,能夠百度下而後通常這個xml都須要有一個defaultCache,就是默認的cache配置 裏面有哪些參數本身能夠網上查查api
而後下面我還配置了一個testCache,我找網上資料 沒看到哪裏明說,而後我本身測試,發現ehcache是能夠生成多個cache的,每一個cache能夠根據不一樣的業務場景做用於不一樣的業務(即裏面的參數配置不一樣),因此這樣看似多配置了,實際上是更加增長了靈活性。
而後在spring的配置文件裏面加上一段配置:
<!-- 使用ehcache緩存 --> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean>
這樣就能夠把ehcache和spring整合起來了
而後在對應的mapper.xml 裏面加上
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" > <property name="timeToIdleSeconds" value="3600"/><!--1 hour--> <property name="timeToLiveSeconds" value="3600"/><!--1 hour--> <property name="maxEntriesLocalHeap" value="1000"/> <property name="maxEntriesLocalDisk" value="10000000"/> <property name="memoryStoreEvictionPolicy" value="LRU"/> </cache>
後面的參數配置不加也能夠,都會有一個默認值,你們也能夠查查一共有哪些配置,而後根據本身的須要來配置,而後這個配置是會帶上cache執行的日誌,若是不要帶日誌能夠把LogginEhcache改爲EhcacheCache。
在mapper.xml這樣設置了默認是所有操做都會執行緩存策略,若是有某些sql不須要執行,能夠把useCache設置爲false。
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.anenjoy.manage.entity.TblUserTempExample" useCache="false" >
其實通過這樣的配置ehcache已經基本OK了。接下來就測試一下。
原文測試代碼以下:
long begin = System.nanoTime(); tempService.selectAll(); long end = System.nanoTime() - begin; System.out.println("count :" + end); // the second time begin = System.nanoTime(); try { tempService.insertTblUserTemp("1", "1", "1", "1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } end = System.nanoTime() - begin; System.out.println("count :" + end); // the second time begin = System.nanoTime(); tempService.selectAll(); end = System.nanoTime() - begin; System.out.println("count :" + end); // the third time begin = System.nanoTime(); tempService.selectAll(); end = System.nanoTime() - begin; System.out.println("count :" + end); return "";
這裏面有4條輸出語句
首先是查詢一次某表 執行了查詢sql
而後給某表插入一條信息 執行了插入sql
而後再查詢某表 執行了查詢sql,這是正確的 由於你執行了非查詢語句,這個時候須要從新訪問數據庫
而後再一次查詢某表 沒有執行查詢sql 這表明緩存是有用的,他就沒有訪問數據庫了,直接從內存或者磁盤裏面獲取了
也能夠根據count來查看操做的時間
count :681719
————————————————-com.anenjoy.manage.mapper.TblUserTempMapper.insert
insert into TBLUSERTEMP (ACTIVECODE, PASSWORDMD5, PASSWORDRANDOMKEY,
PHONE, EMAIL, USERNAME,
AWARTAR, STATUS, CREATEDATE,
USERID, IMSI, CHECKCODE
)
values (?, ?, ?,
?, ?, ?,
?, ?, ?,
?, ?, ?
)
count :129557388
————————————————-com.anenjoy.manage.mapper.TblUserTempMapper.selectByExample
select
ACTIVECODE, PASSWORDMD5, PASSWORDRANDOMKEY, PHONE, EMAIL, USERNAME, AWARTAR, STATUS,
CREATEDATE, USERID, IMSI, CHECKCODE
from TBLUSERTEMP
count :333938203
count :560704
而後這些基本上OK了
原文地址:http://www.darrenzhong.com/?p=73#codesyntax_6
源碼下載:http://pan.baidu.com/s/1jGnATds 解壓密碼:Darren中博客
總結:以上兩種方式我都有試過,也測試過兩種方式的效率。在此發表一下我的觀點,若有其餘觀點歡迎你們討論。
使用ehcache+mybatis的查詢效率不如springMVC+mybatis+ehcache的效率高。
ehcache.xml配置文件中的<diskStore path="java.io.tmpdir"/>節點使用系統默認緩存位置的效率比使用自定義緩存位置 path="d:\cache"的效率要高。
最後附上個人demo源碼:源碼下載
源碼里加了數據庫的sql文件 直接導入便可,以前還試了mybatis數據庫的讀寫分離。可是他們都是獨立存在的,並不會互相影響。下次再寫一篇關於數據庫讀寫分離的吧。
暫時就這些吧。第一次發博文。
2015年3月17日 15:41:14