>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 20590 个阅读者 刷新本主题
 * 贴子主题:  Spring+JPA+ehcache开启二级本地缓存 回复文章 点赞(0)  收藏  
作者:sunshine    发表时间:2020-03-23 23:12:59     消息  查看  搜索  好友  邮件  复制  引用

  

Spring+JPA+ehcache开启二级本地缓存

由于JPA一级缓存为hibernate默认开启,但是二级缓存需要配置多个文件与JAR包.

     一、POM.XML添加jar包        

  1.       <!--开启JPA二级缓存start-->
  2.       < dependency>
  3.           < groupId>org.hibernate </ groupId>
  4.           < artifactId>hibernate-ehcache </ artifactId>
  5.           < version>5.2.12.Final </ version>
  6.       </ dependency>
  7.       < dependency>
  8.           < groupId>net.sf.ehcache </ groupId>
  9.           < artifactId>ehcache-core </ artifactId>
  10.           < version>2.6.11 </ version>
  11.       </ dependency>
  12.       <!--开启JPA二级缓存end-->

       二、Jpa Entity Manager 配置中此配置文件        

  1.         <!-- Jpa Entity Manager 配置 -->
  2.         < bean  id= "entityManagerFactory"  class= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  3.           < property  name= "dataSource"  ref= "dataSource" />
  4.           < property  name= "jpaVendorAdapter"  ref= "hibernateJpaVendorAdapter" />
  5.           < property  name= "packagesToScan"  value= "com.yonyou.ocm" />
  6.           < property  name= "jpaProperties">
  7.             < props>
  8.               <!-- 命名规则 My_NAME->MyName -->
  9.               < prop  key= "hibernate.ejb.naming_strategy">org.hibernate.cfg.DefaultNamingStrategy </ prop>
  10.               < prop  key= "hibernate.show_sql">true </ prop>
  11.               < prop  key= "hibernate.format_sql">false </ prop>
  12.               < prop  key= "hibernate.enable_lazy_load_no_trans">true </ prop>
  13.               <!--二级缓存-->
  14.               < prop  key= "hibernate.cache.use_query_cache">true </ prop>
  15.               < prop  key= "hibernate.cache.use_second_level_cache">true </ prop>
  16.                <!--ENABLE_SELECTIVE,默认值,除非被@Cacheable显式声明要缓存,否则默认不缓存
  17.                     DISABLE_SELECTIVE,除非被@Cacheable显式声明不缓存,否则默认缓存
  18.                     ALL,总是被缓存
  19.                     NONE,总是不缓存
  20.                 -->
  21.               < prop  key= "javax.persistence.sharedCache.mode">ENABLE_SELECTIVE </ prop>
  22.               < prop  key= "hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory </ prop>
  23.             </ props>
  24.           </ property>
  25.         </ bean>

       三、ehcache.xml 本地缓存策略配置文件        

  1.       < ehcache>
  2.            <!-- 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
  3.            path:指定在硬盘上存储对象的路径-->
  4.           < diskStore  path= "java.io.tmpdir"/>
  5.          <!--  一、以下属性是必须的:
  6.             1、name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
  7.             2、maxElementsInMemory:在内存中缓存的element的最大数目。
  8.             3、maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
  9.             4、eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
  10.             5、overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
  11.           二、以下属性是可选的:
  12.             1、timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
  13.             2、timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
  14.             3、diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
  15.             4、diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
  16.             5、diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
  17.             6、memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
  18.           三、缓存的3 种清空策略 :
  19.             1、FIFO ,first in first out (先进先出).
  20.             2、LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
  21.             3、LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。-->
  22.           < defaultCache
  23.                   maxElementsInMemory= "0"
  24.                   eternal= "false"
  25.                   timeToIdleSeconds= "120"
  26.                   timeToLiveSeconds= "120"
  27.                   overflowToDisk= "true"
  28.          />
  29.                <!--
  30.                   cache:为指定名称的对象进行缓存的特殊配置
  31.                   name:指定对象的完整名
  32.               -->
  33.         <!--   <cache name="sampleCache1"
  34.                  maxElementsInMemory="10000"
  35.                  eternal="false"
  36.                  timeToIdleSeconds="300"
  37.                  timeToLiveSeconds="600"
  38.                  overflowToDisk="true"
  39.           />-->
  40.       </ ehcache>

       四、在entity实体上添加注解,属性value 默认true为开启注解,false关闭注解

         点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小
                                    
                                                
----------------------------
原文链接:https://blog.csdn.net/qq314499182/article/details/82313759

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-03-25 14:01:08 重新编辑]
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=2991

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员