Hibernate集成Ehcache(一)

1 篇文章 0 订阅
1 篇文章 0 订阅
[b][color=indigo]最近研究了一下Ehcache集成Spring、Hibernate中用法,Ehcache处理缓存的确是一个很好一个项目,毕竟是被Hibernate和spring两大开源流行框架所支持的嘛spring两大开源流行框架所支持的嘛[/color][/b]

[b]下面以例子来讲解Hibernate如何使用Ehcache:[/b]
首先说明下,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 class="com.xhy.ehcache.domain.Person"/>

</session-factory>


hibernate启用二级缓存、查询缓存及加载ehcache.xml文件,下面几项必须配置

<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" />


[u]
[size=medium][color=green]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)最少使用[/color][/size]
[/u]


测试实现类:

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]执行测试类,只用当第一次执行的时候才从数据库里面去,以后执行去缓存取。
[color=red]工作流程:缓存中有,从缓存中返回;缓存中没有,去数据库取。[/color]
c.setCacheable(true);//启用缓存
c.setCacheRegion("person");//注册ehcache.xml文件缓存名称
[/b][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值