Java Code Examples for org.springframework.data.redis.core.RedisTemplate#setConnectionFactory()

The following examples show how to use org.springframework.data.redis.core.RedisTemplate#setConnectionFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisConfUtils.java    From storm_spring_boot_demo with MIT License 7 votes vote down vote up
public static RedisTemplate buildRedisTemplate(byte[] redisProperties){
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(
            RedisConfUtils.getClusterConfiguration(
                    (RedisProperties) Serializer.INSTANCE.deserialize(redisProperties)));
    RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    jedisConnectionFactory.afterPropertiesSet();

    GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}
 
Example 2
Source File: RedisConfig.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);

    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(mapper);

    template.setValueSerializer(serializer);

    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 3
Source File: RedisConfig.java    From dubbo-spring-boot-learning with MIT License 6 votes vote down vote up
/**
 * RedisTemplate配置,使用Jackson2JsonRedisSerializer作为序列化器
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, String> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    // 使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 4
Source File: RedisCacheConfig.java    From watchdog-framework with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 5
Source File: JsonRedisSerializable.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
*  配置Redis
* @param redisConnectionFactory redis连接工厂
*/
  @Bean
  @ConditionalOnMissingBean(name = "redisTemplate")
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
      RedisTemplate<Object, Object> template = new RedisTemplate<>();
      template.setConnectionFactory(redisConnectionFactory);
      template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
      return template;
  }
 
Example 6
Source File: MybatisAutoConfiguration.java    From utils with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnClass(name = "org.springframework.data.redis.core.RedisTemplate")
@ConditionalOnProperty(name = {"mybatis.cache.type"}, havingValue = "redis", matchIfMissing = true)
public RedisTemplate<byte[], byte[]> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
    template.setConnectionFactory(factory);

    return template;
}
 
Example 7
Source File: RedisConfig.java    From DrivingAgency with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> redisSerializableTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 8
Source File: RedisCacheAutoConfiguration.java    From springboot-learning-experience with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 9
Source File: RedisCacheConfig.java    From syhthems-platform with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    template.setValueSerializer(this.jacksonSerializer);
    template.setKeySerializer(this.stringSerializer);
    template.setHashKeySerializer(this.stringSerializer);
    template.setHashValueSerializer(this.jacksonSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 10
Source File: RedisConfiguration.java    From WTFDYUM with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, Principal> principalRedisTemplate() {
    final RedisTemplate<String, Principal> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(jsonSerializer(Principal.class, objectMapper()));
    template.setValueSerializer(jsonSerializer(Principal.class, objectMapper()));
    return template;
}
 
Example 11
Source File: RedisConfig.java    From MyCommunity with Apache License 2.0 5 votes vote down vote up
@Bean
    // RedisConnectionFactory 连接工厂,Spring会自动注入这个Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(om);

        // 设置key的序列化方式
        template.setKeySerializer(redisSerializer); // 序列化为String
        // 设置value的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
//        // 设置hash的key的序列化方式   当value为hash时,hash有key和value,所以需要设置
//        template.setHashKeySerializer(RedisSerializer.string());
//        // 设置hash的value的序列化方式
//        template.setHashValueSerializer(RedisSerializer.json());
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();  // 使上面设置生效
        return template;
    }
 
Example 12
Source File: RedisConfiguration.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnClass(name = "org.springframework.data.redis.connection.RedisConnectionFactory")
public RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    JdkSerializationRedisSerializer serializationRedisSerializer = new JdkSerializationRedisSerializer();
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(serializationRedisSerializer);
    redisTemplate.setValueSerializer(serializationRedisSerializer);
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
}
 
Example 13
Source File: RedisConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<Long, Book> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Long, Book> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    // Add some specific configuration here. Key serializers, etc.
    return template;
}
 
Example 14
Source File: RedisCacheConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    RedisTemplate<String, String> redis = new RedisTemplate<String, String>();
    GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
    redis.setDefaultSerializer(fastJsonRedisSerializer);
    redis.setConnectionFactory(cf);
    redis.afterPropertiesSet();
    return redis;
}
 
Example 15
Source File: RedisConfiguration.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a configured {@link RedisTemplate}.
 * 
 * @return {@link RedisTemplate} Object, Object
 */
@Bean
public RedisTemplate<Object, Object> redisTemplateObject() {

     RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
     redisTemplate.setConnectionFactory(jedisConnectionFactory());
     redisTemplate.setKeySerializer(new StringRedisSerializer());
     redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
     redisTemplate.setHashKeySerializer(new StringRedisSerializer());
     redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
     return redisTemplate;
}
 
Example 16
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
 */
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 17
Source File: RedisConfig.java    From light-reading-cloud with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> redis = new RedisTemplate<>();
    redis.setConnectionFactory(redisConnectionFactory);
    this.setSerializer(redis);
    return redis;
}
 
Example 18
Source File: MultiCacheAutoConfig.java    From api-gateway-old with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
        RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 19
Source File: RedisRegistryAutoConfiguration.java    From bird-java with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, RouteDefinition> routeRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, RouteDefinition> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);

    GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
    StringRedisSerializer keySerializer = new StringRedisSerializer();
    redisTemplate.setValueSerializer(valueSerializer);
    redisTemplate.setKeySerializer(keySerializer);

    return redisTemplate;
}
 
Example 20
Source File: RedisConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public RedisTemplate<?, ?> redisTemplate(final LettuceConnectionFactory connectionFactory) {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    return template;
}