You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate 6二级缓存:hibernate-ehcache适配Spring Boot 3及替代方案咨询

Spring Boot 3 + Hibernate 6 下替代 hibernate-ehcache 的方案

首先明确:hibernate-ehcache 已停止维护(最后更新为2021年的6.0.0-alpha7),无法适配Hibernate 6的二级缓存SPI改动,在Spring Boot 3环境下无法正常工作。以下是目前可行的替代方案:

1. 基于JCache(JSR-107)的Ehcache 3实现

Ehcache 3本身仍在活跃维护,且完全兼容JCache规范,是Hibernate 6官方推荐的方向之一。Spring Boot 3对JCache有原生支持,配置步骤如下:

  • 引入依赖:
    <!-- Spring Boot Cache starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!-- Ehcache 3 JCache provider -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    <!-- Hibernate JCache适配 -->
    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-jcache</artifactId>
    </dependency>
    
  • 配置application.properties:
    # 开启Hibernate二级缓存
    spring.jpa.properties.hibernate.cache.use_second_level_cache=true
    # 指定JCache缓存区域工厂
    spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.jcache.JCacheRegionFactory
    # 开启选择性缓存实体
    spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
    # 指定Ehcache配置文件路径
    spring.cache.jcache.config=classpath:ehcache.xml
    
  • 实体类上使用Hibernate的@Cache注解标记需要缓存的实体:
    @Entity
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "UserRegion")
    public class User {
        // 实体字段
    }
    

2. Caffeine 二级缓存

Caffeine是一款高性能的本地缓存库,Hibernate 6官方提供了hibernate-caffeine适配器,配置简单且性能优异:

  • 引入依赖:
    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-caffeine</artifactId>
    </dependency>
    
  • 配置application.properties:
    spring.jpa.properties.hibernate.cache.use_second_level_cache=true
    spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.caffeine.CaffeineRegionFactory
    # 配置Caffeine缓存规则,比如最大容量1000,访问后5分钟过期
    spring.jpa.properties.hibernate.cache.caffeine.spec=maximumSize=1000,expireAfterAccess=5m
    
  • 实体类同样使用@Cache注解即可生效。

3. Redis 分布式二级缓存

如果你的应用是分布式部署场景,Redis是更合适的选择,可实现跨节点的缓存共享:

  • 引入依赖(以Redisson JCache实现为例):
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson-hibernate-6</artifactId>
    </dependency>
    
  • 配置application.properties:
    spring.jpa.properties.hibernate.cache.use_second_level_cache=true
    spring.jpa.properties.hibernate.cache.region.factory_class=org.redisson.hibernate.RedissonRegionFactory
    # 指定Redisson配置文件
    spring.jpa.properties.hibernate.cache.redisson.config=classpath:redisson.yaml
    
  • 编写Redisson配置文件,指定Redis连接信息、缓存过期策略等。

注意事项

  • 迁移时需确保原有@Cache注解的缓存策略(如READ_WRITE、NONSTRICT_READ_WRITE)与新缓存实现兼容;
  • 所有依赖版本需与Spring Boot 3、Hibernate 6的版本匹配,避免出现兼容性问题。

内容的提问来源于stack exchange,提问作者mbodev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:42:42