ehcache.xml文件:
下載ehcache,hibernate3.2必須要ehcache1.2以上才能支持。可以修改log4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志
< ehcache>
< diskStore path="c:\\ehcache\"/>
< defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
< !-- 設置Category類的緩存的數據過期策略 -->
< cache name="org.qiujy.domain.cachedemo.Category"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
< !-- 設置Category類的products集合的緩存的數據過期策略 -->
< cache name="org.qiujy.domain.cachedemo.Category.products"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
< cache name="org.qiujy.domain.cachedemo.Product"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
< /ehcache>
配置的元素說明:
元素或屬性 描述
< diskStore> 設置緩存數據文件的存放目錄
< defaultCache> 設置緩存的默認數據過期策略
< cache> 設定具體的命名緩存的數據過期策略
每個命名緩存代表一個緩存區域,每個緩存區域有各自的數據過期策略。命名緩存機制使得用戶能夠在每個類以及類的每個集合的粒度上設置數據過期策略。
cache元素的屬性
name 設置緩存的名字,它的取值為類的全限定名或類的集合的名字
maxInMemory 設置基于內存的緩存中可存放的對象最大數目
eternal 設置對象是否為永久的,true表示永不過期,此時將忽略timeToIdleSeconds和timeToLiveSeconds屬性;
默認值是false
timeToIdleSeconds 設置對象空閑最長時間,超過這個時間,對象過期。當對象過期時,EHCache會把它從緩存中清除。
如果此值為0,表示對象可以無限期地處于空閑狀態。
timeToLiveSeconds 設置對象生存最長時間,超過這個時間,對象過期。
如果此值為0,表示對象可以無限期地存在于緩存中。
overflowToDisk 設置基于內在的緩存中的對象數目達到上限后,是否把溢出的對象寫到基于硬盤的緩存中
------君臨天下,舍我其誰
------