使用带有spring数据缓存的redis时如何启用分布式/集群缓存 [英] How to enable distributed/clustered cache when using redis with spring data cache
问题描述
如何在使用带有 spring-boot
缓存的 Redis 时启用分布式/集群缓存.
How to enable distributed/clustered cache when using Redis with spring-boot
cache.
特别是通过spring-boot-starter-data-redis
推荐答案
在 spring boot 应用程序中启用缓存非常简单.您只需执行三个步骤.
Enable caching in the spring boot app is very simple. You would need to just follow three steps.
- 定义缓存配置
- 将 EnableCaching 添加到任何配置类
- 提供一个 CacheManager bean
对于 Redis,我们有可以配置和创建的 RedisCacheManager.
For Redis, we've RedisCacheManager that can be configured and created.
缓存配置
@Configuration
@Getter
@Setter
@ConfigurationProperties(prefix = "cache")
public class CacheConfigurationProperties {
// Redis host name
private String redisHost;
// Redis port
private int redisPort;
// Default TTL
private long timeoutSeconds;
// TTL per cache, add enties for each cache
private Map<String, Long> cacheTtls;
}
通过属性或 yaml 文件设置它们的值
Set their values via properties or yaml file like
cache.redisHost=localhost
cache.redisPort=6379
cache.timeoutSeconds=1000
cache.cacheTtls.cach1=100
cache.cacheTtls.cach2=200
创建配置后,您可以通过构建器为 RedisCacheManger 创建缓存配置.
Once you have created configuration, you can create cache config for RedisCacheManger by builder.
@Configuration
@EnableCaching
public class CacheConfig {
private static RedisCacheConfiguration createCacheConfiguration(long timeoutInSeconds) {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(timeoutInSeconds));
}
@Bean
public LettuceConnectionFactory redisConnectionFactory(CacheConfigurationProperties properties) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(properties.getRedisHost());
redisStandaloneConfiguration.setPort(properties.getRedisPort());
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisCacheConfiguration cacheConfiguration(CacheConfigurationProperties properties) {
return createCacheConfiguration(properties.getTimeoutSeconds());
}
@Bean
public CacheManager cacheManager(
RedisConnectionFactory redisConnectionFactory, CacheConfigurationProperties properties) {
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
for (Entry<String, Long> cacheNameAndTimeout : properties.getCacheTtls().entrySet()) {
cacheConfigurations.put(
cacheNameAndTimeout.getKey(), createCacheConfiguration(cacheNameAndTimeout.getValue()));
}
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration(properties))
.withInitialCacheConfigurations(cacheConfigurations)
.build();
}
}
如果您使用的是 Redis 集群,请按照此更新缓存属性.在这种情况下,如果您想要缓存特定的 bean 而不是将这些方法设为私有,那么一些 bean 将成为主要的.
If you're using Redis cluster than update cache properties as per that. In this some beans would become primary if you want cache specific bean than make these methods private.
这篇关于使用带有spring数据缓存的redis时如何启用分布式/集群缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!