Hibernate集成Ehcache(一)
最近研究了一下Ehcache集成Spring、Hibernate中用法,Ehcache处理缓存的确是一个很好一个项目,毕竟是被Hibernate和spring两大开源流行框架所支持的嘛spring两大开源流行框架所支持的嘛
下面以例子来讲解Hibernate如何使用Ehcache:
首先说明下,Hibernate的一级缓存都在调用HttpSession中的方法时候由hibernate内部自己处理,不可卸载。
hibernate.cfg.xml文件:
<session-factory> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=mytest</property> <property name="connection.username">sa</property> <property name="connection.password">sa</property> <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property><property name="show_sql">true</property><property name="format_sql">false</property><property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.provider_configuration_file_resource_path">ehcache.xml</property><mapping name="code"><property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.provider_configuration_file_resource_path">ehcache.xml</property>
<defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <cache name="person" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /><!-- 如果你使用了Hibernate的查询缓存,需要在ehcache.xml中加入下面的配置--><cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true" /><cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="120" overflowToDisk="true" />
public static void main(String[] args) {//org.hibernate.cache.EhCache.Session s = HibernateSessionFactory.getSession();Criteria c=s.createCriteria(Person.class); c.setCacheable(false);c.setCacheRegion("person");long s1 = System.currentTimeMillis();List l=c.list();System.out.println("第一次查询:"+UtilTool.getTime(s1));long s2 = System.currentTimeMillis();c.list();System.out.println("第2次查询:"+UtilTool.getTime(s2));for (int i = 0; i < 10; i++) {long s3 = System.currentTimeMillis();c.list();System.out.println("多次("+(i+1)+")查询:"+UtilTool.getTime(s3));}HibernateSessionFactory.closeSession(); s=HibernateSessionFactory.getSession();//s.clear();c=s.createCriteria(Person.class); c.setCacheable(false);c.setCacheRegion("person");long s4 = System.currentTimeMillis();l=c.list();System.out.println("**第一次查询:"+UtilTool.getTime(s4));long s5 = System.currentTimeMillis();c.list();System.out.println("**第2次查询:"+UtilTool.getTime(s5));for (int i = 0; i < 10; i++) {long s6 = System.currentTimeMillis();c.list();System.out.println("多次("+(i+1)+")查询:"+UtilTool.getTime(s6));}/*Query q=s.createQuery("from Person").setCacheable(true) .setCacheRegion("person"); l=q.list();*/ HibernateSessionFactory.closeSession(); }c.setCacheable(true);//启用缓存c.setCacheRegion("person");//注册ehcache.xml文件缓存名称[/b][/size]