springboot同时整合redis和ehcache

本文介绍如何在SpringBoot应用中同时集成Redis和Ehcache,并确保两者工作时互不干扰。通过添加Maven依赖和配置properties文件,实现了缓存管理器对第三方缓存的支持。使用缓存注解时,可以灵活选择本地或Redis存储。同时,尽管RedisTemplate已注入,但可通过自定义实现来满足特定需求。测试表明,DAO层的缓存管理可以通过注解或模板方法自由切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot同时整合redis和ehcache,redis和ehcache各种工作,互补干扰。

1.maven依赖

		<!-- 本地缓存依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<dependency>  
  			<groupId>org.ehcache</groupId>  
  			<artifactId>ehcache</artifactId>  
		</dependency>  
		<dependency>  
  			<groupId>javax.cache</groupId>  
  			<artifactId>cache-api</artifactId>  
		</dependency>  
		

        <!-- redis   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

同时把ehcache的版本设置下,我设置的3.4.0。在maven的属性中添加<ehcache3.version>3.4.0</ehcache3.version>,即可以覆盖父依赖的。

2.在application.properties中添加相关配置。主要是ehcache和redis的

#-------------------ehcache----------------------
spring.cache.type=jcache
spring.cache.jcache.config=classpath:/config/ehcache.xml
spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider

#-------------------redis------------------------
spring.redis.host=ip
spring.redis.port=port
spring.redis.password=pwd
spring.redis.pool.max-active=10
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=5
spring.redis.pool.min-idle=0
spring.redis.timeout=50
spring.redis.ssl=false

3.在添加stater和配置属性后,系统启动的时候两种缓存都会加载。jecache默认为ehcache。

但是我想要的是,我使用缓存注解的时候,指定缓存一处,要么是本地,要么是redis。spring提供了自己的一套缓存管理器来管理其他第三方的缓存。集成接口实现自己的即可。

添加redis的starter的时候,re

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值