Hibernate+ehcache二級緩存技術
1、首先設置EhCache,建立配置文件ehcache.xml,默認的位置在class-path,可以放到你的src目錄下:
2、在Hibernate配置文件中設置:
說明一下:如果不設置“查詢緩存”,那么hibernate只會緩存使用load()方法獲得的單個持久化對象,如果想緩存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法獲得的數據結果集的話,就需要設置
hibernate.cache.use_query_cache true 才行
3、在Hbm文件中添加<cache usage="read-only"/>
4、如果需要“查詢緩存”,還需要在使用Query或Criteria()時設置其setCacheable(true);屬性
5、實踐出真知,給一段測試程序,如果成功的話第二次查詢時不會讀取數據庫
| <?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" <!-- 緩存最大數目 --> eternal="false" <!-- 緩存是否持久 --> overflowToDisk="true" <!-- 是否保存到磁盤,當系統當機時--> timeToIdleSeconds="300" <!-- 當緩存閑置n秒后銷毀 --> timeToLiveSeconds="180" <!-- 當緩存存活n秒后銷毀--> diskPersistent="false" diskExpiryThreadIntervalSeconds= "120"/> </ehcache> |
2、在Hibernate配置文件中設置:
| <!-- 設置Hibernate的緩存接口類,這個類在Hibernate包中 --> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 是否使用查詢緩存 --> <property name="hibernate.cache.use_query_cache">true</property> 如果使用spring調用Hibernate的sessionFactory的話,這樣設置: <!--HibernateSession工廠管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="datasource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value> </list> </property> </bean> |
說明一下:如果不設置“查詢緩存”,那么hibernate只會緩存使用load()方法獲得的單個持久化對象,如果想緩存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法獲得的數據結果集的話,就需要設置
hibernate.cache.use_query_cache true 才行
3、在Hbm文件中添加<cache usage="read-only"/>
4、如果需要“查詢緩存”,還需要在使用Query或Criteria()時設置其setCacheable(true);屬性
5、實踐出真知,給一段測試程序,如果成功的話第二次查詢時不會讀取數據庫
| package cn.rmic.hibernatesample; import java.util.List; import org.hibernate.CacheMode; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory; import cn.rmic.manager.po.Resources; public class testCacheSelectList ...{ /** *//** * @param args */ public static void main(String[] args) ...{ // TODO Auto-generated method stub Session s=HibernateSessionFactory.getSession(); Criteria c=s.createCriteria(Resources.class); c.setCacheable(true); List l=c.list(); // Query q=s.createQuery("From Resources r") // .setCacheable(true) // .setCacheRegion("frontpages") ; // List l=q.list(); Resources resources=(Resources)l.get(0); System.out.println("-1-"+resources.getName()); HibernateSessionFactory.closeSession(); try ...{ Thread.sleep(5000); } catch (InterruptedException e) ...{ // TODO Auto-generated catch block e.printStackTrace(); } s=HibernateSessionFactory.getSession(); c=s.createCriteria(Resources.class); c.setCacheable(true); l=c.list(); // q=s.createQuery("From Resources r").setCacheable(true) // .setCacheRegion("frontpages"); // l=q.list(); resources=(Resources)l.get(0); System.out.println("-2-"+resources.getName()); HibernateSessionFactory.closeSession(); } } |