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的缓存行为。以下是一些常用的配置参数及其含义:
-
spring.jpa.properties.hibernate.cache.use_second_level_cache
- 是否启用二级缓存。设为
true
时,Hibernate会启用二级缓存机制,对实体类进行缓存管理。
- 是否启用二级缓存。设为
-
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
。
- 指定缓存提供者的工厂类。当使用Ehcache时,设置为
-
spring.jpa.properties.hibernate.cache.use_query_cache
- 是否启用查询缓存。设为
true
时,Hibernate会对HQL查询结果进行缓存,提高了重复查询的性能。
- 是否启用查询缓存。设为
-
spring.jpa.properties.hibernate.cache.provider_class
- 在老版本的Hibernate中用于指定缓存提供者类全名,现在通常通过region.factory_class替代。
-
spring.jpa.properties.hibernate.cache.region_prefix
- 缓存区域前缀,用于区分不同实体类或集合的缓存区域。
-
spring.jpa.properties.hibernate.cache.include Collections
- 控制是否缓存集合属性,默认为
all
,表示所有集合属性都会被缓存。
- 控制是否缓存集合属性,默认为
-
spring.jpa.properties.hibernate.cache.use_minimal_puts
- 控制在更新缓存时是否只做最小化的PUT操作,比如仅更新变化的部分,而非整个实体。
-
spring.jpa.properties.hibernate.cache.use_structured_entries
- 是否使用结构化的条目存储在缓存中,结构化意味着存储整个实体对象而非仅仅ID。
-
spring.jpa.properties.hibernate.cache.default_cache_concurrency_strategy
- 设置默认的并发策略,例如
transactional
、read-write
、nonstrict-read-write
等,用于控制缓存对象在并发环境下的读写行为。
- 设置默认的并发策略,例如
-
spring.jpa.properties.hibernate.generate_statistics
- 是否生成Hibernate的统计信息,便于分析缓存效果和数据库交互。
完成上述步骤后,Hibernate的二级缓存将通过Ehcache得以启用。
在运行时,符合缓存策略的实体对象将会在数据库之外的缓存中存储和检索,以提高数据访问性能。
请注意,具体的Ehcache配置应当根据实际需求进行调整。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!