1. maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--redis相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
<!--ehcache3相关依赖-->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.10.8</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2. ehcache.xml配置
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3
http://www.ehcache.org/schema/ehcache-core-3.7.xsd">
<cache-template name="offheap-cache">
<resources>
<offheap unit="MB">10</offheap>
</resources>
</cache-template>
<cache alias="myCache"
uses-template="offheap-cache">
<expiry>
<ttl unit="hours">48</ttl>
</expiry>
</cache>
</config>
3. application.yml配置
spring:
redis:
cluster:
timeout: 1000
max-redirects: 3
nodes: 192.168.31.1:6379,192.168.31.2:6379,192.168.31.3:6379
lettuce:
pool:
max-idle: 10 # 连接池中的最大空闲连接
max-wait: 500 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
cache:
type: jcache
jcache:
config: classpath:ehcache.xml
provider: org.ehcache.jsr107.EhcacheCachingProvider #若存在多个provider时需指定
4. 启动类Application.java需配置@EnableCaching
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5. 实例化RedisCacheManager和JCacheCacheManager
@Configuration
@Slf4j
public class CacheConfig {
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1)); // 设置缓存有效期1天
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public JCacheManagerFactoryBean cacheManagerFactoryBean() throws Exception {
JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
jCacheManagerFactoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
return jCacheManagerFactoryBean;
}
@Bean
@Primary
public JCacheCacheManager ehcacheManager() throws Exception {
final JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
jCacheCacheManager.setCacheManager(cacheManagerFactoryBean().getObject());
return jCacheCacheManager;
}
}
6. 使用@Cacheable时指定cacheManager
由于在实例化ehcacheManager时有配置@Primary注解,所以ehcache的@Cacheable可以不用指定cacheManager
@Service
public class TestService {
@Cacheable(value = "redisCache", cacheManager = "redisCacheManager")
public String getRedisCache(String key) {
return "redis:" + key;
}
@Cacheable(value = "myCache", cacheManager = "ehcacheManager")
public String getEhcacheCache(String key) {
return "ehcache:" + key;
}
}
7. 其他问题
如果你还集成了redisson,那么一定要在application.yml中配置spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider,否则会报错有多个Provider