hibernate (十一 緩存Ehcache 采用annoation)
Posted on 2009-08-24 17:09 瘋狂 閱讀(683) 評論(0) 編輯 收藏 所屬分類: hibernate從hibernate2.1開始ehcache已經作為hibernate的默認緩存方案(二級緩存方案 sessionfactory級別), 在項目中有針對性的使用緩存將對性能的提升右很大的幫助。
要使用 Ehcache:需要一下步驟
一,classpath添加相應的jar(ehcache,commons-logging)
二,然后在hibernate.cfg.xml中配置
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
說明:如果沒有配置<property name="cache.use_second_level_cache">true</property>(默認false) 將會產生根據單個id查詢的情況(產生很多sql)。
三,為需要緩存的類添加緩存標示:
使用mapping文件時需要添加node :
@Entity @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
如果使用使用hibernate annoation是使用@Cache(usage=CacheConcurrencyStrategy.)標簽,有5種可選的緩存方案:
1,CacheConcurrencyStrategy.NONE
不適用,默認
2. CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
更新不頻繁幾個小時或更長
3,CacheConcurrencyStrategy.READ_ONLY
對于不發生改變的數據使用
4,CacheConcurrencyStrategy.READ_WRITE
基于時間戳判定機制,,對于數據同步要求嚴格的情況,使用頻繁
5,CacheConcurrencyStrategy.TRANSACTIONAL
運行在jta環境種,基于事務
四,在classpath下添加ehcache.xml
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000" <!-- 緩存最大數目 -->
eternal="false" <!-- 緩存是否持久 -->
overflowToDisk="true" <!-- 是否保存到磁盤,當系統當機時-->
timeToIdleSeconds="300" <!-- 當緩存閑置n秒后銷毀 -->
timeToLiveSeconds="180" <!-- 當緩存存活n秒后銷毀-->
diskPersistent="false"
diskExpiryThreadIntervalSeconds= "120"/>
</ehcache>
測試:
@Entity @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) public class B { private int id; private String bname; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public int getId() { return id; } public String getBname() { return bname; } ... }
并配置到cfg文件中:<mapping class="com.eric.po.B" />
main方法:
public static void main(String[] args) throws Exception { getTest(); getTest(); }
public static void getTest() throws Exception { Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from B where id>?"); q.setParameter(0, 10); q.setCacheable(true); 需要設置此屬性 List list = q.list(); for (Iterator iterator = list.iterator(); iterator.hasNext();) { B a2 = (B) iterator.next(); System.out.print(a2.getId() + "/"); } HibernateSessionFactory.closeSession(); }
控制臺信息:
Hibernate: select b0_.id as id1_, b0_.bname as bname1_ from B b0_ where b0_.id>? 11/14/18/25/26/27/28/29/Hibernate: select b0_.id as id1_, b0_.bname as bname1_ from B b0_ where b0_.id>? 11/14/18/25/26/27/28/29/
只發出了一次sql 第二次從緩存中取
我們配置我們自己的緩存文件:
<cache name="cache_a" maxElementsInMemory="5" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
我們將maxElementsInMemory設置小一點 我們就可以看見磁盤的緩存文件:
首先說明:我們在ehcache.xml <diskStore path="java.io.tmpdir"/>配置了緩存溢出到的磁盤路徑
可以通過:
System.out.println(System.getProperty("java.io.tmpdir"));
查詢。
測試代碼:
public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("java.io.tmpdir")); getTest(); getTest(); Thread.sleep(10000);
}
我們在最后暫停10秒來查看磁盤文件
public static void getTest() throws Exception { Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from B where id>?"); q.setParameter(0, 10); q.setCacheable(true); q.setCacheRegion("cache_a");//使用我們自己配置的緩存 List list = q.list(); for (Iterator iterator = list.iterator(); iterator.hasNext();) { B a2 = (B) iterator.next(); System.out.print(a2.getId() + "/"); } HibernateSessionFactory.closeSession(); }
控制臺信息:
C:\DOCUME~1\eric\LOCALS~1\Temp\ //我的java.io.tmpdir Hibernate: select b0_.id as id1_, b0_.bname as bname1_ from B b0_ where b0_.id>? 11/14/18/25/26/27/28/29/Hibernate: select b0_.id as id1_, b0_.bname as bname1_ from B b0_ where b0_.id>? 11/14/18/25/26/27/28/29/
查看磁盤信息:
在文件按目錄下有一下文件: com.eric.po.B.data--------0kb cache_a.data ------------4 kb org.hibernate.cache.StandardQueryCache.data ---0kb org.hibernate.cache.UpdateTimestampsCache.data -----0kb
其中 cache_a中保存了我們的緩存文件
StandardQueryCache.data 則是 設置默認的查詢緩存的數據過期策略 產生的文件,
org.hibernate.cache.UpdateTimestampsCache.data則是 設置時間戳緩存的數據過期策略
如果不適用我們自己的緩存配置就會使用類類的全路徑路徑文件(com.eric.po.B.data)來緩存我們的數據。