Hibernate二级缓存和查询缓存

二级缓存:

  1. jar包两个
    commons-logging.jar
    ehcache-1.2.3.jar
  2. 总配置文件
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
		<mapping resource="entity/dept.hbm.xml" />

这里写图片描述
ehcache.xml

<ehcache>

    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

		<!--Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。-->
		<!--eternal:是否是永恒数据,如果是,则它的超时设置会被忽略。-->
		<!--timeToIdleSeconds:对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。-->
        <!--timeToLiveSeconds:对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。-->
		<!--overflowToDisk:如果内存中数据数量超过maxElementsInMemory限制,是否要缓存到磁盘上。-->
    <cache name="entity.Emp"
        maxElementsInMemory="10000"  
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <cache name="entity.Dept"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> 

</ehcache>

查询缓存:
二级缓存仍然只能缓存get()/load()方法获取的session数据;
如果想要缓存HQL语句的session数据,需要采用查询缓存(use-_query_cache)。

  1. 开启了二级缓存
  2. 总配置文件
<property name="hibernate.cache.use_query_cache">true</property>
  1. HQL查询时,加入query.setcatchable(true)
    Emp e=(Emp) query.setCacheable(true).uniqueResult();

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值