mybatis - 快取
阿新 • • 發佈:2018-11-19
快取說明:快取中有查詢的資料,就快取中取,沒有就資料庫中取並同步快取
順序: 查詢 ---> 二級快取 ---> 一級快取 ---> 資料庫
------------------------- 一級快取 -----------------------------
***
一級快取是 session 級別的
預設開啟
無法配置
資料的變更 會更新快取 並清除之前 session 的快取
------------------------- 二級快取 -----------------------------
sqlMapConfig.xml 配置檔案中
<settings>
<!-- 延遲載入的總開關-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- aggressiveLazyLoading 設定成false才是啟用延遲載入 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 開啟二級快取,在mybatis中只要快取的配置都指的是二級快取 -->
<setting name="cacheEnabled" value="true"/>
</settings>
對應對映檔案中
implements Serializable 對應的實體類繼承Serializable 生成 uuid
<!-- 當前對映檔案開啟二級快取 -- 使用ehcache框架
不設定type預設使用自帶二級快取策略 -->
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
瞭解即可:
select:useCache:是否啟用二級快取true是啟用false是禁用,預設是true
select:flushCache:是否重新整理快取(清掉快取)true是重新整理 false不重新整理,預設是true
insert:flushCache:是否重新整理快取(清掉快取)true是重新整理 false不重新整理,預設是true
update:flushCache:是否重新整理快取(清掉快取)true 是重新整理 false不重新整理,預設是true
建立 ehcache.xml 二級快取配置檔案
## 需要ehcache依賴包 mybatis-ehcache-1.0.2.jar + ehcache-core-2.6.5.jar ##
<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.
-->
<!--
maxElementsInMemory:快取物件個數
eternal:快取是否永久有效
timeToIdleSeconds:閒置時間
timeToLiveSeconds:有效時間
overflowToDisk:記憶體滿了就儲在硬碟
MaxElementsOnDisk:硬碟上的最大時間
diskPersisent:硬碟上是否永久有效
memoryStoreEvctionPolicy:LRC(最近最少使用),FIFO(先進先出)
-->
<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>