mybatis整合ehcache分布式快取框架

2021-08-09 20:46:34 字數 3104 閱讀 7840

mybatis提供了乙個cache介面,如果要實現自己的快取邏輯,實現cache介面開發即可。

mybatis和ehcache整合,mybatis和ehcache整合包中提供了乙個cache介面的實現類。

<

>

<

cache

type='org.mybatis.caches.ehcache.ehcachecache'

>

cache

>

可以根據需求調整快取引數:

<

cache

type='org.mybatis.caches.ehcache.ehcachecache'

>

<

property

name='timetoidleseconds'

value='3600'

/>

<

property

name='timetoliveseconds'

value='3600'

/>

<

property

name='maxentrieslocalheap'

value='1000'

/>

<

property

name='maxentrieslocaldisk'

value='10000000'

/>

<

property

name='memorystoreevictionpolicy'

value='lru'

/>

cache

>

在classpath下配置ehcache.xml

<

ehcache

xmlns:xsi=''

xsi:nonamespaceschemalocation='../config/ehcache.xsd'

>

<

diskstore

path='f:\develop\ehcache'

/>

<

defaultcache

maxelementsinmemory='1000'

maxelementsondisk='10000000'

eternal='false'

overflowtodisk='false'

timetoidleseconds='120'

timetoliveseconds='120'

diskexpirythreadintervalseconds='120'

memorystoreevictionpolicy='lru'

>

defaultcache

>

ehcache

>

屬性說明:

diskstore

:指定資料在磁碟中的儲存位置。

defaultcache

:當借助

cachemanager.add('democache')

建立cache

時,ehcache

便會採用

指定的的管理策略

以下屬性是必須的:

maxelementsinmemory - 

在記憶體中快取的

element

的最大數目

maxelementsondisk - 

在磁碟上快取的

element

的最大數目,若是

0表示無窮大

eternal - 

設定快取的

elements

是否永遠不過期。如果為

true

,則快取的資料始終有效,如果為

false

那麼還要根據

timetoidleseconds

,timetoliveseconds判斷

overflowtodisk- 

設定當記憶體快取溢位的時候是否將過期的

element

快取到磁碟上

以下屬性是可選的:

timetoidleseconds - 

當快取在

ehcache

中的資料前後兩次訪問的時間超過

timetoidleseconds

的屬性取值時,這些資料便會刪除,預設值是

0,也就是

可閒置時間無窮大

timetoliveseconds - 

快取element

的有效生命期,預設是

0.,也就是

element

存活時間無窮大

diskspoolbuffersizemb 

這個引數設定

diskstore(

磁碟快取

)的快取區大小

.預設是

30mb.

每個cache

都應該有自己的乙個緩衝區.

diskpersistent在vm

重啟的時候是否啟用磁碟儲存

ehcache

中的資料,預設是

false。

diskexpirythreadintervalseconds - 

磁碟快取的清理執行緒執行間隔,預設是

120秒。每個

120s

,相應的執行緒會進行一次

ehcache

中資料的清理工作

memorystoreevictionpolicy - 

當記憶體快取達到最大,有新的

element

加入的時候,

移除快取中

element

的策略。預設是

lru(最近最少使用),可選的有

lfu(最不常使用)和

fifo

(先進先出)

對於訪問多的查詢請求且使用者對查詢結果實時性要求不高,此時可採用mybatis二級快取技術降低資料庫訪問量,提高訪問速度,業務場景比如:耗時較高的統計分析

sql、**賬單查詢sql等。

實現方法如下:通過設定重新整理間隔時間,由mybatis每隔一段時間自動清空快取,根據資料變化頻率設定快取重新整理間隔flushinterval,比如設定為30分鐘、60分鐘、24小時等,根據需求而定。

MyBatis快取之二級快取 ehcache整合

二級快取允許手動管理 首先,二級快取可以跨session,只要在乙個sessionfactory範圍之內則允許跨session,但要想使用二級快取,需要手動進行配置 在 sqlmapconfig.xml 配置檔案中新增如下配置 包括之前的配置我一併貼上來了 也就是說,在mybatis框架中,只要是關...

ehcach快取問題

下面是我快取的配置檔案 我的測試 是 cachemanager manager1 new cachemanager ehcache.xml cache memoryonlycache manager1.getcache signal for int i 0 i 100000 i system.out...

mybatis整合ehcache方法

今天剛剛接觸到mybatis整合ehcache的方法 得知道ehcache可以說是乙個快取框架,用來對二級快取的資料進行集中管理。同時還需要到匯入mybatis與ehcache的相應的包 ehcache core 2.6.5.jar mybatis ehcache 1.0.2.jar 在配置完後在建...