shiro+mybatis+ehcache詳細配置 快取降低資料庫壓力
1.首先:pom.xml需要的jar
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
2.新增ehcache.xml的配置檔案
<?xmlversion="1.0"encoding="UTF-8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"monitoring="autodetect"
dynamicConfig="true">
<diskStorepath="java.io.tmpdir"/>
<!-- <diskStore>==========當記憶體快取中物件數量超過maxElementsInMemory時,將快取物件寫到磁碟快取中(需物件實現序列化介面)
* <diskStore path="">==用來配置磁碟快取使用的物理路徑,Ehcache磁碟快取使用的檔案字尾名是*.data和*.index
* name=================快取名稱,cache的唯一標識(ehcache會把這個cache放到HashMap裡)
* maxElementsOnDisk====磁碟快取中最多可以存放的元素數量,0表示無窮大
* maxElementsInMemory==記憶體快取中最多可以存放的元素數量,若放入Cache中的元素超過這個數值,則有以下兩種情況
* 1)若overflowToDisk=true,則會將Cache中多出的元素放入磁碟檔案中
* 2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素
* eternal==============快取中物件是否永久有效,即是否永駐記憶體,true時將忽略timeToIdleSeconds和timeToLiveSeconds
* timeToIdleSeconds====快取資料在失效前的允許閒置時間(單位:秒),僅當eternal=false時使用,預設值是0表示可閒置時間無窮大,此為可選屬性
* 即訪問這個cache中元素的最大間隔時間,若超過這個時間沒有訪問此Cache中的某個元素,那麼此元素將被從Cache中清除
* timeToLiveSeconds====快取資料在失效前的允許存活時間(單位:秒),僅當eternal=false時使用,預設值是0表示可存活時間無窮大
* 即Cache中的某元素從建立到清楚的生存時間,也就是說從建立開始計時,當超過這個時間時,此元素將從Cache中清除
* overflowToDisk=======記憶體不足時,是否啟用磁碟快取(即記憶體中物件數量達到maxElementsInMemory時,Ehcache會將物件寫到磁碟中)
* 會根據標籤中path值查詢對應的屬性值,寫入磁碟的檔案會放在path資料夾下,檔案的名稱是cache的名稱,字尾名是data
* diskPersistent=======是否持久化磁碟快取,當這個屬性的值為true時,系統在初始化時會在磁碟中查詢檔名為cache名稱,字尾名為index的檔案
* 這個檔案中存放了已經持久化在磁碟中的cache的index,找到後會把cache載入到記憶體
* 要想把cache真正持久化到磁碟,寫程式時注意執行net.sf.ehcache.Cache.put(Element element)後要呼叫flush()方法
* diskExpiryThreadIntervalSeconds==磁碟快取的清理執行緒執行間隔,預設是120秒
* diskSpoolBufferSizeMB============設定DiskStore(磁碟快取)的快取區大小,預設是30MB
* memoryStoreEvictionPolicy========記憶體儲存與釋放策略,即達到maxElementsInMemory限制時,Ehcache會根據指定策略清理記憶體
* 共有三種策略,分別為LRU(最近最少使用)、LFU(最常用的)、FIFO(先進先出) -->
<defaultCacheeternal="false"maxElementsInMemory="1000"
overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"
timeToLiveSeconds="600"memoryStoreEvictionPolicy="LRU"/>
<!-- 注意,以下快取是永久有效,是系統初始化資料到快取中,如果不需要永久有效,請另寫,或在 -->
<cache name="cache"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
</cache>
<!-- 登入記錄快取 鎖定10分鐘 -->
<cachename="passwordRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<!--
<cache name="authorizationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<cache name="authenticationCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
-->
<cachename="shiro-activeSessionCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<cachename="shiro-kickout-session"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
</ehcache>
3.在spring.xml中加入encache的配置<!--shiro快取管理器 -->
<beanid="cacheManager"class="com.jd.shiro.spring.SpringCacheManagerWrapper">
<propertyname="cacheManager"ref="springCacheManager"/>
</bean>
<beanid="springCacheManager"class="org.springframework.cache.ehcache.EhCacheCacheManager">
<propertyname="cacheManager"ref="ehcacheManager"/>
</bean>
<!--ehcache-->
<beanid="ehcacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<propertyname="configLocation"value="classpath:ehcache.xml"/>
<propertyname="shared"value="true"></property>
</bean>
4.在mybatis mapper.xml 檔案里加入配置
<cachetype="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
第一種 是帶日誌的 第二種不帶日誌
假如某個業務是不要快取的,可以在當前業務下加上useCache="false"