二級快取
-
Hiberante二級快取
Hibernate的快取:
一級快取:session級快取
二級快取:sessionFactory級快取,用處很大(執行緒級:ehcache)
三級快取:分布式快取(行程級:redis) -
快取分類
- 物體快取:根據物體的ID進行快取,快取的是一個一個的物件
- 查詢快取:根據HQL查詢陳述句進行快取,快取的是結果集,setCacheable(true)
快取:是很多個map(key, value)組成,使用region來區別
-
ehcache使用
1.引入ehcache的相關jar包
ehcache-2.10.3.jar hibernate-ehcache-5.3.8.Final.jar slf4j-api-1.7.7.jar- 撰寫ehcache.xml組態檔
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <diskStore path="../ehcache/hibernate"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="3600" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="600"/> <!-- hibernate內置的快取區域 --> <cache name="org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true"/> <cache name="org.hibernate.cache.internal.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true"/> <!-- 物體物件定制快取,沒定制的物體使用默認設定 --> <cache name="***.Student" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="7200" overflowToDisk="true"/> <cache name="***.Clazz" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="7200" overflowToDisk="true"/> <cache name="***.Clazz.students" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="7200" overflowToDisk="true"/> <!-- 手動使用快取 --> <cache name="mycache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="7200" overflowToDisk="true"/> </ehcache>- 在hibernate.cfg.xml中開啟二級快取,指定EhCache
<property name="cache.use_second_level_cache">true</property> <property name="cache.use_query_cache">true</property> <property name="cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>- 在物體類上標注@Cache注解
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) -
Hibernate的快取會自動同步更新
-
快取什么時候失效
- 使用update/delete陳述句進行批量修改時,會導致個快取(region)全部失效
- 使用save(), update()會區域更新某個快取(region)
- 使用原生SQL的update/delete,也會自動同步快取
-
快取什么時候更新
- 直接修改資料庫的資料,快取是不會同步的(避免直接修改資料庫)
-
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/147747.html
標籤:其他
下一篇:深入oracle索引,磁區
