首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Hibernate集成Ehcache(1)

2013-10-07 
Hibernate集成Ehcache(一)最近研究了一下Ehcache集成Spring、Hibernate中用法,Ehcache处理缓存的确是一个很

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>



ehcache.xml
<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" />


  
name: cache的名字,用来识别不同的cache,必须惟一。  
maxElementsInMemory: 内存管理的缓存元素数量最大限值。  
maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。  
eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。  
overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。    
timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。  
timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。  
diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。  
diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。  
diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。  
memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用



测试实现类:
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(); }

[size=medium][b]执行测试类,只用当第一次执行的时候才从数据库里面去,以后执行去缓存取。
工作流程:缓存中有,从缓存中返回;缓存中没有,去数据库取。
c.setCacheable(true);//启用缓存c.setCacheRegion("person");//注册ehcache.xml文件缓存名称
[/b][/size]

热点排行