maven工程中hibernate的二级缓存ehcache的配置

      说到缓存对于新手来说可能是一件很头疼的事儿,做为新手的我,今天给大家分享下自己的学习过程。最近项目中用到了可配置参数,这类参数基本无需修改,除了新增,刚开始是自己写了一个mapCache,就是用ConcurrentHashMap在系统启动时将所有数据缓存到map中,这样避免了系统运行时频繁访问数据库查询。但是这里有个缺点,如果系统启动后在管理平台新增参数那么新增的参数不会在map中,业务系统必须重启重新加载参数。所以这个方法不能满足系统的需求。之后又想到了定时器,Spring中自带的定时功能,定时没隔5分钟查询数据库并更新Map中的配置参数,这个方法确实可以解决问题,但是增加的系统的负担。最后想到了hibernate的二级缓存,这个缓存一直没有用过,工作快两年了,真对不起自己的职业,决定研究下。切入主题:
     1.首先说下hibernate的配置,如下
    <prop key="hibernate.cache.use_query_cache">true</prop>     是否开启查询缓存,此处true表示开启
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 是否开启二级缓存,true表示开启
    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
    <!-- Hibernate4.0以上设置factory  -->
    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
    </prop>
    <!-- 二级缓存 ehcache的配置文件路径  -->
    <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache-hibernate.xml</prop>
2.ehcache-hibernate.xml 的配置,如下,文件中写的已经很清楚,不在做说明
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
 <!--磁盘缓存位置: 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
 <diskStore path="java.io.tmpdir/ehcache" />
 <!-- 默认的管理策略 maxElementsOnDisk: 在磁盘上缓存的element的最大数目,默认值为0,表示不限制。 eternal:设定缓存的elements是否永远不过期。
  如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds判断:当缓存闲置n秒后销毁; timeToLiveSeconds判断:当缓存存活n秒后销毁;
  overflowToDisk:是否保存到磁盘,当系统当机时; diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
  diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。 标识对象状态(过期/持久化)的线程多长时间运行一次。 -->
 <!-- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。 -->
 <defaultCache
     maxElementsInMemory="10000"
     eternal="false"
  timeToIdleSeconds="3600"
  timeToLiveSeconds="3600"
  overflowToDisk="true"
  diskPersistent="false"
  diskExpiryThreadIntervalSeconds="120"
  memoryStoreEvictionPolicy="LRU">
 </defaultCache>
 
  <!--想使用查询缓存,这两个类需要添加-->
    <cache
        name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        overflowToDisk="true">
    </cache>
     <cache
      name="org.hibernate.cache.internal.StandardQueryCache"
      maxElementsInMemory="10000"
      eternal="false"
      timeToLiveSeconds="120"
      overflowToDisk="true">
     </cache>
 <!-- Special objects setting. -->
 <cache
     name="com.cosw.entity.huaian.PayRates"
     maxElementsInMemory="20"
  eternal="false"
  diskPersistent="false"
  overflowToDisk="false"
  timeToIdleSeconds="600"
  timeToLiveSeconds="600"
  maxElementsOnDisk="1000000"
  memoryStoreEvictionPolicy="LRU" >
 </cache>
 
 <cache
     name="com.cosw.entity.huaian.SysParam"
     maxElementsInMemory="20"
  eternal="false"
  diskPersistent="false"
  overflowToDisk="false"
  timeToIdleSeconds="600"
  timeToLiveSeconds="600"
  maxElementsOnDisk="1000000"
  memoryStoreEvictionPolicy="LRU" >
 </ca
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值