天天看點

Hibernate二級緩存完整配置思路

HIbernate包含一級緩存與二級緩存,一級緩存這裡不做介紹,着重講解二級緩存的配置:

思路:

什麼是 hibernate二級緩存?

怎麼配置hibernate的二級緩存?

如何使用hibernate二級緩存結合自己的項目?

hibernate配置二級緩存的具體思路:

1,  導入hibernate及其緩存的相關依賴;
2,  修改spring-hibernate.xml的配置檔案(hibernate.properties),包含緩存産品的驅動類;
3, 建立 enache.xml檔案,添加需要緩存的model;
4,   方式一:  建立Person.hbm.xml檔案; 方式二:annotation注解實作;
5,   測試程式;
           

hibernate 結合項目實作的具體思路:

1,導入jar包或者添加依賴;
2,修改spring-hibernate.xml;
3,配置hibernate.properties;
4,Src下添加ehcache.xml;
5,Hibernate.hbm.xml或者entity的實體類添加注解(4個政策);
6,hibernate的load(),iterate(),list()添加 setCacheAble(true);
7,功能測試,可以實作;
           

以下是結合本項目實作的具體思路:

第一步: pom檔案添加依賴,為友善管理,version處一般會集中處理,最後諸如這種形式展示:${hibernate.version}

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-ehcache</artifactId>
     <version>5.2.10.Final</version>
</dependency>
           

第二步: 修改spring-hibernate.xml配置檔案,

<!-- 配置Hibernate Session工廠 -->
    <b:bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<b:property name="dataSource" ref="dataSource" />
        <b:property name="hibernateProperties">
            <b:props>
                <!-- 系統使用的資料庫方言 -->
                <b:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</b:prop>
                <!-- 是否列印Hibernate生成的SQL到控制台 -->
                <b:prop key="hibernate.show_sql">${pom.hibernate.show_sql}</b:prop>
                <!-- 是否格式化列印出來的SQL -->
                <b:prop key="hibernate.format_sql">${pom.hibernate.format_sql}</b:prop>
                <!-- 是否二級緩存最小化寫操作   新增--> 
                <b:prop key="hibernate.cache.use_minimal_puts">true</b:prop>
                <!-- 是否使用hibernate的二級緩存 新增 -->
                <b:prop key="hibernate.cache.use_second_level_cache">true</b:prop>
                <!-- 是否使用查詢緩存   新增 -->
                <b:prop key="hibernate.cache.use_query_cache">true</b:prop>
                <!-- 指定使用緩存産品的驅動類  新增-->
                <b:prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</b:prop>
                <!-- ehcache區域工廠  新增  -->
                <b:prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</b:prop>
            </b:props>
        </b:property>
           

第三步: 配置hibernate.properties配置檔案,本項目已經移除,主要是配合spring-hibernate.xml 新增的props使用;

第四步:添加encache.xml配置檔案

<!-- 
	defaultCache節點為預設的緩存政策
    maxElementsInMemory 記憶體中最大允許存在的對象數量
    eternal 設定緩存中的對象是否永遠不過期
    overflowToDisk:當緩存對象達到1000個的時候,是否把溢出的對象存放到硬碟上
    timeToIdleSeconds:指定緩存對象空閑多長時間就過期,過期的對象會被清除掉
    timeToLiveSeconds:指定緩存對象總的存活時間,超過這個值就會被清除掉
    diskPersistent:當你的緩存應用關閉的時候,是否需要把緩存的對象持久化到硬碟上。當jvm結束是是否持久化對象
    diskExpiryThreadIntervalSeconds:指定專門用于清除過期對象的監聽線程的輪詢時間,也就是說後面有一個線程,它會不斷掃描,掃描是否有對象過期,有對象過期,就會将其清除掉
 -->
<!--  <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> -->
<ehcache>
	<diskStore path="user.home/ehcache/" />
	<defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true" 
		timeToIdleSeconds="120" 
		timeToLiveSeconds="180"
		diskPersistent="false" 
		diskExpiryThreadIntervalSeconds="120"
		MemoryStoreEvictionPolicy="LRU" />
	<cache name="com.cccollector.user.model.SensitiveWord" maxElementsInMemory="100000" eternal="false"
   		   overflowToDisk="true" 
   		   timeToIdleSeconds="86400" 
   		   timeToLiveSeconds="86400" 
   		   diskPersistent="false"
   		   MemoryStoreEvictionPolicy="LRU"/>
</ehcache>
           

第五步:Hibernate.hbm.xml或者entity的實體類添加注解(4個政策),本項目使用如下政策

//表示開啟二級緩存,并使用read-only政策
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
           

第五步新增 實體Bean的配置方式有三種:

(1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

 (2).hibernate.cfg.xml中标簽配置方式: <class-cache class="" usage="" />

 (3).映射檔案*.hb.xml中标簽配置方式: <cache usage=" />

           

第六步:hibernate的load(),iterate(),list()添加 setCacheAble(true)

此方法使用的前提: hibernate,且使用的是JPA注解方式;

第七步: 測試

至此花費三天的成果進行展示,實作的具體的步驟你可以不用過細揣摩,最重要的,最重要的,最重要的,是整體的思路,思路,思路;

感謝我們老大給我時間讓我去研究,希望你們也能夠做事,思路先行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Hibernate二級緩存完整配置思路
Hibernate二級緩存完整配置思路
Hibernate二級緩存完整配置思路
Hibernate二級緩存完整配置思路

繼續閱讀