访问ElasticCache - Jedis和spring

时间:2017-11-15 18:27:21

标签: spring spring-data jedis amazon-elasticache

我们刚开始使用AWS,并要求将AWS ElasticCache与Redis-jedis一起使用。 Spring-data-redis 1.8.8.RELEASE aws-java-sdk 1.11.228 春季4.2.9.RELEASE jedis 2.9.0

我能够使用以下代码将数据连接并缓存到本地redis。我尝试将代码更改为https://github.com/fishercoder1534/AmazonElastiCacheExample/tree/master/src/main/java,但未成功。非常感谢一些指导和一些示例代码的帮助。 AWS ElasticCache当前配置为选项1,但也需要尽快转到选项2。 1.非复制群集 - Redis群集已禁用,没有副本 2.已复制的群集 - 已启用Redis群集,并且已使用只读副本禁用Redis群集。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import org.springframework.cache.interceptor.KeyGenerator;
import java.lang.reflect.Method;
import java.util.List;

@Configuration
@EnableCaching
// @PropertySource("classpath:/redis.properties")
public class CacheConfig extends CachingConfigurerSupport {
// private @Value("${redis.host}") String redisHost;
// private @Value("${redis.port}") int redisPort;

//@Bean
  public KeyGenerator keyGenerator() {
    return new KeyGenerator() {
      @Override
      public Object generate(Object o, Method method, Object... objects) {
        // This will generate a unique key of the class name, the method name, and all method parameters appended.
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(method.getName());
        for (Object obj : objects) {
          sb.append(obj.toString());
        }
        return sb.toString();
      }
    };
  }


@Bean
public JedisConnectionFactory redisConnectionFactory() {
    JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();

    // Defaults for redis running on Local Docker 
    redisConnectionFactory.setHostName("192.168.99.100");
    redisConnectionFactory.setPort(6379);

    return redisConnectionFactory;
}

@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(cf);
    redisTemplate.setDefaultSerializer(new JdkSerializationRedisSerializer()); 
    return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);

    // Number of seconds before expiration. Defaults to unlimited (0)
    cacheManager.setDefaultExpiration(1200);
    cacheManager.getCacheNames().forEach(cacheM-> {System.out.println(cacheM);});
    return cacheManager;
}

}

0 个答案:

没有答案