需要兩個(gè)包:
commons-logging-1.0.4.jar
ehcache-1.2.3.jar

在src根目錄下新建 ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
 
<diskStore path="java.io.tmpdir" />
 
<defaultCache maxElementsInMemory="10000" eternal="false"
  timeToIdleSeconds
="120" timeToLiveSeconds="120" overflowToDisk="true"
  diskExpiryThreadIntervalSeconds
="120" memoryStoreEvictionPolicy="LRU" />
 
<cache name="cn.yu.vo.Guestbook" maxElementsInMemory="1000" eternal="false"
  timeToIdleSeconds
="3000" timeToLiveSeconds="600" overflowToDisk="true"/>
</ehcache>
  <!-- 
          diskStore:保存在硬盤(pán)上的臨時(shí)目錄
          
          name:Cache的唯一標(biāo)識(shí)

          maxElementsInMemory:內(nèi)存中最大緩存對(duì)象數(shù)。

          maxElementsOnDisk:磁盤(pán)中最大緩存對(duì)象數(shù),若是0表示無(wú)窮大。

          eternal:Element是否永久有效,一但設(shè)置了,timeout將不起作用。

          overflowToDisk:配置此屬性,當(dāng)內(nèi)存中Element數(shù)量達(dá)到maxElementsInMemory時(shí),Ehcache將會(huì)Element寫(xiě)到磁盤(pán)中。

          timeToIdleSeconds:設(shè)置Element在失效前的允許閑置時(shí)間。僅當(dāng)element不是永久有效時(shí)使用,可選屬性,默認(rèn)值是0, 
                                              也就是可閑置時(shí)間無(wú)窮大。

          timeToLiveSeconds:設(shè)置Element在失效前允許存活時(shí)間。最大時(shí)間介于創(chuàng)建時(shí)間和失效時(shí)間之間。僅當(dāng)element不是永久
                                              有效時(shí)使用,默認(rèn)是0.,  也就是element存活時(shí)間無(wú)窮大。

          diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù)。(這個(gè)虛擬機(jī)是指什么虛擬機(jī)一直沒(méi)看明白是什么,有高人還希望能指點(diǎn)
                                       一二)。

          diskExpiryThreadIntervalSeconds:磁盤(pán)失效線程運(yùn)行時(shí)間間隔,默認(rèn)是120秒。

          diskSpoolBufferSizeMB:這個(gè)參數(shù)設(shè)置DiskStore(磁盤(pán)緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個(gè)Cache都應(yīng)該有自己的一
                                                     個(gè)緩沖區(qū)。

          memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是
                                                           LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。這里比較
                                                           遺憾,Ehcache并沒(méi)有提供一個(gè)用戶定制策略的接口,僅僅支持三種指定策略,感覺(jué)做的不夠
                                                           理想。
 
-->

在hibernate.cfg.xml里面新增屬性
    <property name="cache.provider_class">
        org.hibernate.cache.EhCacheProvider
    
</property>
在guestbook.hbm.xml里面新增屬性
    <class name="cn.yu.vo.Guestbook" table="GUESTBOOK" schema="OLIVER">
    
<cache usage="read-write"/>
        
<id name="id" type="java.lang.Long">
            
<column name="ID" precision="22" scale="0" />
            
<generator class="sequence">
             
<param name="sequence">gb_seq</param>
             
</generator>
        
</id>