1. maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--redis相關(guān)依賴-->
<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相關(guān)依賴-->
<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 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
min-idle: 0 # 連接池中的最小空閑連接
cache:
type: jcache
jcache:
config: classpath:ehcache.xml
provider: org.ehcache.jsr107.EhcacheCachingProvider #若存在多個(gè)provider時(shí)需指定
4. 啟動(dòng)類Application.java需配置@EnableCaching
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5. 實(shí)例化RedisCacheManager和JCacheCacheManager
@Configuration
@Slf4j
public class CacheConfig {
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1)); // 設(shè)置緩存有效期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時(shí)指定cacheManager
由于在實(shí)例化ehcacheManager時(shí)有配置@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. 其他問(wèn)題
如果你還集成了redisson,那么一定要在application.yml中配置spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider,否則會(huì)報(bào)錯(cuò)有多個(gè)Provider