1. 程式人生 > >hibernate二級只讀快取設定 --oracle 是對sessionFactory而言的

hibernate二級只讀快取設定 --oracle 是對sessionFactory而言的

配置二級快取的檔案   hibernate.cfg.xml 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <!-- 配置oracle驅動 -->
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
  <property name="hibernate.connection.username">scott</property>
  <!-- 方言 -->
  <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
  <!-- 輸出列印sql語句 -->
  <property name="hibernate.show_sql">true</property>
  <!-- 配置session的獲取方式 currentSession-->
  <property name="hibernate.current_session_context_class">thread</property>
  <!--     <property name="hibernate.use_sql_comments">true</property>
 sql語句格式化輸出 -->
  <property name="hibernate.format_sql">true</property>
  <!-- 是否建立表 沒有表-->

  <property name="hibernate.hbm2ddl.auto">update</property>

   <!-- 是否啟用耳機快取 -->
  <property name="hibernate.cache.use_second_level_cache">true</property>
 <!--  設定實現二級快取的實現類 -->
  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
 
  <!-- 註冊實體的對映檔案 -->
  <mapping resource="com/hu/po/HibernateUser.hbm.xml"/>
 <!--  設定啟用二級快取的實現類的只讀(查詢)快取 -->

  <class-cache usage="read-only" class="com.hu.po.HibernateUser"/>

 </session-factory>

</hibernate-configuration>

ehcache.xml檔案  專案路徑

<ehcache>

 
    <diskStore path="./target/tmp"/>
<-- 這些資料是通過壓力測試  負荷是執行負荷的一倍時測得的值-->
    <defaultCache
            maxElementsInMemory="10000"  
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
            
            <!-- maxElementsInMemory  Cache中最多允許儲存的資料物件的數量 -->
              <!-- external=“false” //快取中物件是否為永久的,如果是,超時設定將被忽略,物件從不過期 -->
              <!-- timeToIdleSeconds=“1000”  //快取資料鈍化時間(設定物件在它過期之前的空閒時間)   -->
              <!-- timeToLiveSeconds=“1000” 快取資料的生存時間(設定物件在它過期之前的生存時間)-->
              <!-- overflowToDisk=“false” />    //記憶體不足時,是否啟用磁碟快取   -->
              <!-- memoryStoreEvictionPolicy="LRU" //記憶體不足時資料物件的清除策略
                  ehcache中快取的3種清空策略:
                 FIFO(first in first out):先進先出
                 LFU( Less Frequently Used):一直以來最少被使用的。如上面所講,快取的元素有一個hit屬性,
                                             hit值最小的將會被清出快取。
                 LRU(Least Recently Used):最近最少使用的,快取的元素有一個時間戳,當快取容量滿了 ,
                                             而又需要騰出地方來快取新的元素的時候,那麼現有快取元素中
                                             時間戳離當前時間最遠的元素將被清出快取。
             -->
</ehcache>

檔案路徑

所需jar包