|
Spring+JPA+ehcache开启二级本地缓存由于JPA一级缓存为hibernate默认开启,但是二级缓存需要配置多个文件与JAR包.
一、POM.XML添加jar包
- <!--开启JPA二级缓存start-->
- < dependency>
- < groupId>org.hibernate </ groupId>
- < artifactId>hibernate-ehcache </ artifactId>
- < version>5.2.12.Final </ version>
- </ dependency>
- < dependency>
- < groupId>net.sf.ehcache </ groupId>
- < artifactId>ehcache-core </ artifactId>
- < version>2.6.11 </ version>
- </ dependency>
- <!--开启JPA二级缓存end-->
|
二、Jpa Entity Manager 配置中此配置文件
- <!-- Jpa Entity Manager 配置 -->
- < bean id= "entityManagerFactory" class= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- < property name= "dataSource" ref= "dataSource" />
- < property name= "jpaVendorAdapter" ref= "hibernateJpaVendorAdapter" />
- < property name= "packagesToScan" value= "com.yonyou.ocm" />
- < property name= "jpaProperties">
- < props>
- <!-- 命名规则 My_NAME->MyName -->
- < prop key= "hibernate.ejb.naming_strategy">org.hibernate.cfg.DefaultNamingStrategy </ prop>
- < prop key= "hibernate.show_sql">true </ prop>
- < prop key= "hibernate.format_sql">false </ prop>
- < prop key= "hibernate.enable_lazy_load_no_trans">true </ prop>
- <!--二级缓存-->
- < prop key= "hibernate.cache.use_query_cache">true </ prop>
- < prop key= "hibernate.cache.use_second_level_cache">true </ prop>
- <!--ENABLE_SELECTIVE,默认值,除非被@Cacheable显式声明要缓存,否则默认不缓存
- DISABLE_SELECTIVE,除非被@Cacheable显式声明不缓存,否则默认缓存
- ALL,总是被缓存
- NONE,总是不缓存
- -->
- < prop key= "javax.persistence.sharedCache.mode">ENABLE_SELECTIVE </ prop>
- < prop key= "hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory </ prop>
- </ props>
- </ property>
- </ bean>
|
三、ehcache.xml 本地缓存策略配置文件
- < ehcache>
- <!-- 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
- path:指定在硬盘上存储对象的路径-->
- < diskStore path= "java.io.tmpdir"/>
- <!-- 一、以下属性是必须的:
- 1、name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
- 2、maxElementsInMemory:在内存中缓存的element的最大数目。
- 3、maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
- 4、eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
- 5、overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
- 二、以下属性是可选的:
- 1、timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
- 2、timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
- 3、diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
- 4、diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
- 5、diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
- 6、memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
- 三、缓存的3 种清空策略 :
- 1、FIFO ,first in first out (先进先出).
- 2、LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
- 3、LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。-->
- < defaultCache
- maxElementsInMemory= "0"
- eternal= "false"
- timeToIdleSeconds= "120"
- timeToLiveSeconds= "120"
- overflowToDisk= "true"
- />
- <!--
- cache:为指定名称的对象进行缓存的特殊配置
- name:指定对象的完整名
- -->
- <!-- <cache name="sampleCache1"
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- overflowToDisk="true"
- />-->
- </ ehcache>
|
四、在entity实体上添加注解,属性value 默认true为开启注解,false关闭注解

----------------------------
原文链接:https://blog.csdn.net/qq314499182/article/details/82313759
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 flybird 在 2020-03-25 14:01:08 重新编辑]
|
|