SSH开启Hibernate二级缓存

1、添加ehcache依赖:

<!-- Hibernate二级缓存依赖 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

2、配置Spring.xml: 

<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>

<!-- 二级缓存实现类 -->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

<!-- 启用查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>

<!-- 加载缓存所需配置文件 -->
<prop key="hibernate.net.sf.ehcache.configurationResourceName">classpath:ehcache.xml</prop>

-------------------------------------------------------------------------------------------

<!-- 配置hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <!-- 使用查询缓存(使用二级缓存需要需要在hibernateTemplate添加此property) -->
        <property name="cacheQueries" value="true"/>
    </bean>

3、创建ehcache.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<ehcache>
    <diskStore path="D:\cache"/>
    <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
                  timeToIdleSeconds="120"
                  timeToLiveSeconds="180"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="60"/>

<!-- 具体到某个类的数据过期策略 -->      
<!-- 
<cache name="com.wulianfa.model.User"                                                 maxElementsInMemory="10000"  
eternal="false"          
timeToIdleSeconds="300"          
timeToLiveSeconds="600"          
overflowToDisk="true"/>
-->

</ehcache>

<!--diskStore:指定缓存的对象存放在硬盘上的哪个路径底下。
defaultCache:定义缓存的默认行为,即为缺省的缓存策略。
maxElementsInMemory:在缓存中默认存放的元素(对象)个数,即内存中最大允许存在的对象数量。
eternal:设置缓存中的对象是否永远不过期,true代表永远不过期,一直存在,false代表要过期了。
overflowToDisk:当缓存对象达到内存中最大允许存在的数量时,是否把溢出的对象存放到硬盘上。
timeToIdleSeconds:指定缓存对象空闲多长时间就过期,过期的对象会被清除掉。
timeToLiveSeconds:指定缓存对象总的存活时间,超过这个值就会被清除掉。
diskPersistent:当你的缓存应用关闭的时候,是否需要把缓存的对象持久化到硬盘上,即当JVM结束时是否持久化对象。
diskExpiryThreadIntervalSeconds:指定专门用于清除过期对象的监听线程的轮询时间,也就是说后面有一个线程,它会不断扫描,扫描是否有对象过期,有对象过期,就会将其清除掉。-->

4、配置hbm.xml:

<class name="com.wulianfa.model.User" table="t_user">
        <!-- 注明需要使用二级缓存的实体类 usage :缓存策略 region :指定缓存区域名(实体类全称) -->
        <cache usage="read-write" region="com.wulianfa.model.User" />
        <!-- id -->
        <id name="userId" column="ID">
            <generator class="native"></generator>
        </id>
        <!-- 用户 -->
        <property name="userName" column="NAME"></property>
        <!-- 密码 -->
        <property name="userPassword" column="PASSWORD"></property>
    </class>

 

end...

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值