【问题标题】:Redis cache docker container with spring boot not working on my local Machine带有spring boot的Redis缓存docker容器在我的本地机器上不起作用
【发布时间】:2019-02-02 02:44:06
【问题描述】:

我在本地机器上使用 redis 缓存作为 docker 映像。我已经使用可缓存的 anthion 为我的一种方法启用了缓存。当我在 aws 上使用它而不是 localhost 时,应用程序无法缓存相同的内容

public class RedisConfig {

    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {

        System.out.println("localhost")
        System.out.println("6379");
        jedisConnectionFactory.getHostName();
        jedisConnectionFactory.getPort();

        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        // Number of seconds before expiration. Defaults to unlimited (0)
        cacheManager.setDefaultExpiration(60);
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }

}
@Cacheable(value="sgcode" , cacheManager ="cacheManager")
    public String getSegmentCode(String aname ) {
        Logger.info("code ", "##### SEGMENT METHOD CALLED ##### {}", aname);
        return segmentCodeMap.get(aname);       
    }

Logger line will be printed only once after that it should fetch from cache.

【问题讨论】:

    标签: spring docker spring-boot redis


    【解决方案1】:

    经过一番挣扎,终于找到了根本原因。在应用程序类中使用 enablecaching 注解

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2021-11-19
      • 1970-01-01
      • 2018-07-17
      • 2020-02-26
      • 2020-05-29
      • 2018-02-02
      • 2017-11-28
      • 1970-01-01
      相关资源
      最近更新 更多