【问题标题】:How to enable distributed/clustered cache when using redis with spring data cache使用带有spring数据缓存的redis时如何启用分布式/集群缓存
【发布时间】:2021-01-06 15:26:47
【问题描述】:

如何在使用带有spring-boot 缓存的 Redis 时启用分布式/集群缓存。

尤其是通过spring-boot-starter-data-redis使用Redis时

【问题讨论】:

    标签: spring-boot redis spring-data spring-data-redis spring-cache


    【解决方案1】:

    在 Spring Boot 应用中启用缓存非常简单。您只需执行三个步骤。

    • 定义缓存配置
    • 将 EnableCaching 添加到任何配置类
    • 提供一个 CacheManager bean

    对于 Redis,我们有可配置和创建的 RedisCacheManager。

    缓存配置

    @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文件设置它们的值

    cache.redisHost=localhost
    cache.redisPort=6379
    cache.timeoutSeconds=1000
    cache.cacheTtls.cach1=100
    cache.cacheTtls.cach2=200
    

    创建配置后,您可以通过 builder 为 RedisCacheManger 创建缓存配置。

    @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 将成为主要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 2020-01-06
      • 2017-07-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多