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