Redis缓存docker容器,弹簧启动不能在我的本地机器上运行

问题描述 投票:0回答:1

我在本地计算机上使用redis缓存作为docker镜像。我使用可缓存的anothion为我的一个方法启用了缓存。当我在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个回答
0
投票

经过多次斗争后找到了根本原因。在应用程序类中使用enablecaching批注

© www.soinside.com 2019 - 2024. All rights reserved.