S2SH整合以及图片上传(4)之二级缓存的配置

    上面一篇文章,我们说完了hibernate的搭建,接下来,我们来配置一下,二级缓存。

    二级缓存指的是是SessionFactory级别的缓存,是属于进程范围的缓存。

    第一步,我们仍然需要导入相关的jar包,如下勾选的ehcache-core-2.4.3.jar:

    

    这里的ehcache-core-2.4.3.jar包,在我们进行hibernate的搭建的时候就已经放进去了,这里只是重新提一下,需要大家注意,当在使用二级缓存的时候,不要忘记了这个jar包。

    第二步,由于hibernate的核心已经被spring给接管了,所以我们需要在spring的配置文件applicationContext.xml中配置二级缓存,如下:


    其中代码放置到配置会话工厂的bean中,代码如下:

	<!-- 配置二级缓存 -->
  hibernate.cache.use_second_level_cache=true
  hibernate.cache.use_query_cache=false
  hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
  hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

    第三步,在src目录下新建一个ehcache.xml文件:

    

    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.ssh.po.Person" maxElementsInMemory="100" eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           diskPersistent="false"/>
</ehcache>
    第四步,对于要进行二级缓存的实体类,进行配置
   

    其中代码如下:

<!-- 配置二级缓存 -->
<cache usage="read-write" region="com.ssh.po.Person"/>

    至此,二级缓存配置完毕。

    S2SH整合以及图片上传(5)之struts2的搭建

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值