hibernate緩存

一.hibernate有一級緩存,二級緩存,和查詢緩存。其中一二級緩存都是用來緩存對象,查詢緩存是用來緩存屬性。

一級

1.一級緩存的數據結構是Map,用於存儲查詢實體。Map的key存放實體的Id,Map的value存放實體本身。所以一級緩存無法存儲查詢的屬性。
2.一級緩存的生命週期與Session有關,Session產生一級緩存創建,Session關閉一級緩存銷燬
3.Get,Load,Iterator方法讀寫一級緩存,List方法只寫不讀一級緩存
4.一級緩存不能禁用,但可以通過Session的clear方法(清除所有)和evict方法(指定類清除)清理一級緩存,從而達到禁止寫緩存的效果
5.Session的save方法會寫(一級)緩存,在批量插入數據時要注意對一級緩存做定時清理。Hibernate的批量處理效率不高,建議使用Session.connection()或得Jdbc連接後使用Jdbc的相關API做批處理操作
二級

1.二級緩存也稱進程級的緩存或SessionFactory級的緩存,二級緩存可以被所有的session共享。
2.二級緩存的生命週期和SessionFactory的生命週期一致,SessionFactory可以管理二級緩存。
3.Hibernate默認情況下是打開的,提供一個Hashtable存儲二級緩存,但只適用於研發,開發中爲第三方緩存組件提供了接入接口,我們可以根據不同情況選擇不同的實現。
4.二級緩存也只能緩存實體對象,不緩存屬性。
5.可以通過session.setCacheMode()  CacheMode.PUTCacheMode.GETCacheMode.NORMAL來設置二級緩存
6.ehcache二級緩存的配置和使用:

  * 將echcache.xml文件拷貝到src下

  * 開啓二級緩存,修改hibernate.cfg.xml文件

  <propertyname="hibernate.cache.use_second_level_cache">true</property>

  * 指定緩存產品提供商,修改hibernate.cfg.xml文件

  <propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

  * 指定那些實體類使用二級緩存(兩種方法)

  * 在映射文件中採用<cache>標籤

  * 在hibernate.cfg.xml文件中,採用<class-cache>標籤

如:

[html] view plaincopy
  1. <hibernate-configuration>  
  2.     <session-factory>  
  3.     <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>   
  4.     <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>   
  5.     <property name="hibernate.connection.username">scott</property>   
  6.     <property name="hibernate.connection.password">tiger</property>   
  7.     <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>  
  8.     <property name="format_sql">true</property>  
  9.     <property name="show_sql">true</property>  
  10.     <property name="hibernate.cache.use_second_level_cache">true</property>  
  11.     <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>    
  12.     <mapping resource="com/pk/po/Person.hbm.xml"/>  
  13.     <class-cache usage="read-only" class="com.pk.po.Person"/>   
  14.     </session-factory>  
  15. <!--<session-factory>  
  16.     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateCache</property>   
  17.     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>   
  18.     <property name="hibernate.connection.username">root</property>   
  19.     <property name="hibernate.connection.password">mysql</property>   
  20.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  21.     <property name="format_sql">true</property>  
  22.     <property name="show_sql">true</property>  
  23.     <property name="hibernate.cache.use_second_level_cache">true</property>  
  24.     <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>    
  25.     <mapping resource="com/pk/po/Person.hbm.xml"/>  
  26.     <class-cache usage="read-only" class="com.pk.po.Person"/>   
  27.     </session-factory> -->  
  28. </hibernate-configuration>  

注意:save在註釋的那部分配置信息中是無效的,也就是對二級緩存無效,但可以再oracle中有效

查詢緩存

1.查詢緩存是針對普通屬性結果集的緩存,對實體對象的結果集只緩存id
2.查詢緩存的生命週期爲當前關聯的表發生修改,那麼查詢緩存生命週期結束
3.查詢緩存的配置和使用:

  *在hibernate.cfg.xml文件中啓用查詢緩存,如:

  <property name="hibernate.cache.use_query_cache">true</property>

  * 指定緩存產品提供商,修改hibernate.cfg.xml文件

  <propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

  *在程序中必須手動啓用查詢緩存,如:

  query.setCacheable(true);

4.List方法讀寫查詢緩存,Iterator不使用查詢緩存
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章