Hibernate使用Ehcache二级缓存

在Spring Boot项目中使用Hibernate配合Ehcache作为二级缓存,需要以下几个步骤:

步骤1:引入相关依赖

在项目的pom.xml文件中添加Ehcache和Hibernate-Ehcache的支持。

<dependencies>
    <!-- Spring Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Hibernate core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

    <!-- Ehcache for Hibernate second level cache -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <!-- 替换为对应版本,此处为举例 -->
        <version>2.10.6</version>
    </dependency>
    <!-- For Hibernate 5.x use the following instead -->
    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.4.32.Final</version>
    </dependency> -->

    <!-- If using Hibernate 5.x and Ehcache 3.x -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <!-- 替换为对应版本,此处为举例 -->
        <version>3.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
        <!-- 替换为对应版本,此处为举例 -->
        <version>5.4.32.Final</version>
    </dependency>
</dependencies>

步骤2:配置Hibernate和Ehcache

在application.properties或application.yml中配置Hibernate的二级缓存:

# application.properties
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
# 或者对于Hibernate 5.x和Ehcache 3.x
# spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.JCacheRegionFactory
spring.jpa.properties.hibernate.generate_statistics=true # 可选,开启Hibernate统计信息收集
spring.jpa.properties.hibernate.cache.provider_configuration_file_resource_path=classpath:ehcache.xml # 指定Ehcache配置文件路径

步骤3:配置Ehcache ehcache.xml

创建ehcache.xml配置文件(根据所使用的Ehcache版本不同,配置方式会有所差异):

<!-- ehcache.xml -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 定义一个默认的全局缓存模板 -->
    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <!-- 针对特定实体类的缓存配置 -->
    <cache name="com.example.yourpackage.YourEntity"
           maxEntriesLocalHeap="1000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           statistics="true">
    </cache>
</ehcache>

memoryStoreEvictionPolicy缓存的算法:
    内存中的对象装满之后,新来的对象要把原来的某个对象替换

- lru:(某个:指的是最近很少使用的,在内存中,每个对象都有一个时间标识,在某个对象被访问之后,时间会更新,在经过对对象的 时间排序之后,时间最远的兑现将会被替换)   
    - lfu:(缓存对象执行率低的被替换)
    - fifo:(先进先出)

步骤4:启用缓存注解

在实体类上使用@Cacheable注解启用二级缓存:

import javax.persistence.Cacheable;
import javax.persistence.Entity;

@Entity
@Cacheable
public class YourEntity {
    // ...
}

步骤5:(可选)配置Spring Data JPA的Repository

如果你使用Spring Data JPA,可以在Repository接口上使用@EnableCaching注解开启缓存支持,或者在Spring Boot的主配置类上启用缓存。

@EnableCaching
@SpringBootApplication
public class Application {
    // ...
}

更多hibernate.cache 配置参数详解

Spring Boot在整合Hibernate时,可以通过spring.jpa.properties.hibernate.cache.*系列配置来定制Hibernate的缓存行为。以下是一些常用的配置参数及其含义:

  1. spring.jpa.properties.hibernate.cache.use_second_level_cache

    • 是否启用二级缓存。设为true时,Hibernate会启用二级缓存机制,对实体类进行缓存管理。
  2. spring.jpa.properties.hibernate.cache.region.factory_class

    • 指定缓存提供者的工厂类。当使用Ehcache时,设置为org.hibernate.cache.ehcache.EhCacheRegionFactory(针对Hibernate 4.x)。对于Hibernate 5.x及Ehcache 3.x,应设置为org.hibernate.cache.jcache.JCacheRegionFactory
  3. spring.jpa.properties.hibernate.cache.use_query_cache

    • 是否启用查询缓存。设为true时,Hibernate会对HQL查询结果进行缓存,提高了重复查询的性能。
  4. spring.jpa.properties.hibernate.cache.provider_class

    • 在老版本的Hibernate中用于指定缓存提供者类全名,现在通常通过region.factory_class替代。
  5. spring.jpa.properties.hibernate.cache.region_prefix

    • 缓存区域前缀,用于区分不同实体类或集合的缓存区域。
  6. spring.jpa.properties.hibernate.cache.include Collections

    • 控制是否缓存集合属性,默认为all,表示所有集合属性都会被缓存。
  7. spring.jpa.properties.hibernate.cache.use_minimal_puts

    • 控制在更新缓存时是否只做最小化的PUT操作,比如仅更新变化的部分,而非整个实体。
  8. spring.jpa.properties.hibernate.cache.use_structured_entries

    • 是否使用结构化的条目存储在缓存中,结构化意味着存储整个实体对象而非仅仅ID。
  9. spring.jpa.properties.hibernate.cache.default_cache_concurrency_strategy

    • 设置默认的并发策略,例如transactionalread-writenonstrict-read-write等,用于控制缓存对象在并发环境下的读写行为。
  10. spring.jpa.properties.hibernate.generate_statistics

    • 是否生成Hibernate的统计信息,便于分析缓存效果和数据库交互。

完成上述步骤后,Hibernate的二级缓存将通过Ehcache得以启用。
在运行时,符合缓存策略的实体对象将会在数据库之外的缓存中存储和检索,以提高数据访问性能。
请注意,具体的Ehcache配置应当根据实际需求进行调整。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值