------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------
二級緩存
Mybatis中,預設二級緩存是開啟的。可以關閉。
一級緩存開啟的。可以被解除安裝嗎?不可以的。一級緩存不可以解除安裝,天然和架構綁定。
内置二級緩存
由于MyBatis從緩存中讀取資料的依據與SQL的id相關,而非查詢出的對象。是以,使用二級緩存的目的,不是在多個查詢間共享查詢結果(所有查詢中隻要查詢結果中存在該對象,
就直接從緩存中讀取,這是對查詢結果的共享,Hibernate中的緩存就是為了在多個查詢間共享查詢結果,但MyBatis不是),而是為了防止同一查詢(相同的Sql id,相同的sql語句)的反複執行。
一級緩存: 基于PerpetualCache 的 HashMap本地緩存,其生命周期為 Session,當 Session flush 或 close 之後,該Session中的所有 Cache 就将清空。
二級緩存與一級緩存其機制相同,預設也是采用 PerpetualCache,HashMap存儲,不同在于其存儲作用域為 Mapper(Namespace),并且可自定義存儲源,如 Ehcache
對于緩存資料更新機制,當某一個作用域(一級緩存Session/二級緩存Namespaces)進行了 C/U/D 操作後,預設該作用域下所有 select 中的緩存将被clear
------------------------------------------------------------------------------------------------------------------------------------------------------------》
内置二級緩存的使用簡單配置步驟
1.cacheEnabled=true;(這個預設值就是true,想用二級緩存可以不做配置,不想用改成false,在大配置中)
<settings>
<setting name="cacheEnabled" value="true"/><!--開啟,他也是預設值-->
<!--<setting name="cacheEnabled" value="false"/>--><!--關閉-->
</settings>
2.實體類實作序列化接口Serializeble
由于我做的是關聯查詢,并且沒有做延遲加載,是以倆個實體類都得實作Serializeble接口
3.在接口同名的xml小配置中
加入一個自閉和的<cache/>
内置二級緩存存在性證明
都已經知道一級緩存是sqlsession的級别,如果要是close掉,換成不同的sqlsession怎麼辦?
/*二級緩存*/
@Test
public void t4SecondCacheHasExist(){
SqlSession session= MyBatisUtils.getSession();
IDeptDAO mapper = session.getMapper(IDeptDAO.class);
Dept depts = mapper.findDeptnoALLEmpsMoreSql(1);
System.out.println(depts.getDeptName());
session.close();
System.out.println("===================我是高冷的分割線=====================");
SqlSession session2= MyBatisUtils.getSession();
IDeptDAO mapper2 = session2.getMapper(IDeptDAO.class);
Dept depts2 = mapper2.findDeptnoALLEmpsMoreSql(1);
System.out.println(depts2.getDeptName());
session2.close();
}
運作結果

但由于他對資料庫隻發了上面的sql,分割線後就沒有去資料庫去查找了,而sqlsession關閉掉了,并且又開了一個,說明二級緩存真的存在,是真的運用上了
-----------------------------------------------------------------------------------》
二級緩存參數配置
在接口同名xml小配置中,可以指定具體的二級緩存參數配置
我以代碼加注釋的方式來解釋一波
<!--
eviction:清理緩存策咯,預設值LRU,最近最少使用先清除
flushInterval:重新整理間隔,預設不設定,就是永久
size:對象,預設1024
readOnly:隻讀,預設false
-->
<!--二級緩存-->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
eviction:清理緩存策咯,預設值LRU,最近最少使用先清除,此處設定的FIFO是先進先出的隊列形式,先進來的先清除
flushInterval:重新整理間隔,預設不設定,就是永久,機關毫秒
size:對象,預設1024
readOnly:隻讀,預設false
------------------------------------------------------------------------------------》
二級緩存可以在一條sql語句上設定他的不開啟-----------》局部關閉
useCache改為false,不使用,不開啟
方法如下
<!--設定二級緩存單條sql失效-->
<select id="findDeptnoALLEmpsMoreSql" resultMap="DeptMoreSqlMapper" useCache="false">
SELECT deptNo,deptName FROM dept WHERE deptNo=#{deptNo}
</select>
-------------------------------------------------------------------------------------》
第三方的二級緩存引用---ehcache
使用方式
1.引入jar包,我給你們提供節點
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
<!--MyBatis整合EhCache的包-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
2.小配置中添加<cache/>具體配置,引用到第三方的緩存
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
3.引入配置xml檔案 ehcache.xml 名字一定這麼取,别的不可以
裡面内容
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
接着我們調用剛才二級緩存存在性證明的那個方法,測試引用到了第三方架構ehcache了沒
看到這個表示成功
Cache Hit Ratio 緩存命中率
--------------------------------孤傲的程式員------------------------------------------------------------------------------------------------