Java Code Examples for org.springframework.data.redis.serializer.StringRedisSerializer

The following examples show how to use org.springframework.data.redis.serializer.StringRedisSerializer. These examples are extracted from open source projects. 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 Project: seckill-rocketmq   Source File: RedisCacheConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    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);
    //使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 2
Source Project: java-tutorial   Source File: RedisLock.java    License: MIT License 6 votes vote down vote up
/**
 * get 取值
 *
 * @param key
 * @return
 */
public String get(final String key) {
    Object object = null;
    try {
        object = redisTemplate.execute((RedisCallback<Object>) redisConnection -> {
            StringRedisSerializer redisSerializer = new StringRedisSerializer();
            byte[] data = redisConnection.get(Objects.requireNonNull(redisSerializer.serialize(key)));


            redisConnection.close();
            if (data == null) {
                return null;
            }
            return redisSerializer.deserialize(data);
        });
    } catch (Exception ex) {
        logger.error("get redis error, key : {}", key);
    }

    return object != null ? object.toString() : null;
}
 
Example 3
Source Project: FEBS-Security   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "redisTemplate")
@SuppressWarnings({"unchecked", "rawtypes"})
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    //使用 fastjson 序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value 值的序列化采用 fastJsonRedisSerializer
    template.setValueSerializer(fastJsonRedisSerializer);
    template.setHashValueSerializer(fastJsonRedisSerializer);
    // key 的序列化采用 StringRedisSerializer
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());

    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 4
Source Project: smart-admin   Source File: SmartRedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate() {
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 5
Source Project: ywh-frame   Source File: RedisCacheConfig.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 解决注解方式存放到redis中的值是乱码的情况
 * @param factory 连接工厂
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

    // 配置注解方式的序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration =
            config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
                    //配置注解默认的过期时间
                    .entryTtl(Duration.ofDays(1));
    // 加入白名单   https://github.com/alibaba/fastjson/wiki/enable_autotype
    ParserConfig.getGlobalInstance().addAccept("com.ywh");
    ParserConfig.getGlobalInstance().addAccept("com.baomidou");
    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example 6
Source Project: open-capacity-platform   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Primary
@Bean("redisTemplate")
// 没有此属性就不会装配bean 如果是单个redis 将此注解注释掉
@ConditionalOnProperty(name = "spring.redis.cluster.nodes", matchIfMissing = false)
public RedisTemplate<String, Object> getRedisTemplate() {
	RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
	redisTemplate.setConnectionFactory(lettuceConnectionFactory);

	RedisSerializer stringSerializer = new StringRedisSerializer();
	// RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
	RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
	redisTemplate.setKeySerializer(stringSerializer); // key的序列化类型
	redisTemplate.setHashKeySerializer(stringSerializer);
	redisTemplate.setValueSerializer(redisObjectSerializer); // value的序列化类型
	redisTemplate.setHashValueSerializer(redisObjectSerializer); // value的序列化类型
	redisTemplate.afterPropertiesSet();

	redisTemplate.opsForValue().set("hello", "wolrd");
	return redisTemplate;
}
 
Example 7
Source Project: shield-ratelimter   Source File: RedisCacheConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    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);
    //使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    LOGGER.info("Springboot RedisTemplate 加载完成");
    return template;
}
 
Example 8
Source Project: FEBS-Cloud   Source File: FebsLettuceRedisAutoConfigure.java    License: Apache License 2.0 6 votes vote down vote up
@Bean(name = "redisTemplate")
@ConditionalOnClass(RedisOperations.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(mapper);

    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringRedisSerializer);
    template.setHashKeySerializer(stringRedisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();

    return template;
}
 
Example 9
Source Project: demo-project   Source File: RedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "redisTemplate")
@SuppressWarnings("all")
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    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);

    // 设置RedisTemplate
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(serializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 10
Source Project: yue-library   Source File: RedisAutoConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <p>支持FastJson进行Redis存储对象序列/反序列化
 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 */
@Bean
public RedisTemplate<String, Object> yueRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
	RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
	redisTemplate.setConnectionFactory(redisConnectionFactory);
	
	// 支持FastJson进行Redis存储对象序列/反序列化
	if (redisProperties.getRedisSerializerEnum() != RedisSerializerEnum.JDK) {
		redisTemplate.setDefaultSerializer(redisProperties.getRedisSerializerEnum().getRedisSerializer());
	}
	StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
	redisTemplate.setKeySerializer(stringRedisSerializer);
	redisTemplate.setHashKeySerializer(stringRedisSerializer);
	
	return redisTemplate;
}
 
Example 11
Source Project: demo-project   Source File: RedisConfig.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("all")
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 12
Source Project: spring-boot-study   Source File: RedisConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate<Object,Object> template = new RedisTemplate<>();
    //设置连接池
    template.setConnectionFactory(redisConnectionFactory);
    StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
    //FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<Object>(Object.class);
    //也可以使用 Jackson2JsonRedisSerializer
    //Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    //value 的序列号 使用 fastjson
    template.setValueSerializer(stringRedisSerializer);
    //hash value的序列化问题
    template.setHashValueSerializer(stringRedisSerializer);
    //key 的序列化
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());

    return  template;
}
 
Example 13
/**
    * 序列化Java对象
    * @Author  科帮网
    * @param factory
    * @return  RedisTemplate<Object, Object>
    * @Date	2017年8月13日
    * 更新日志
    * 2017年8月13日 科帮网  首次创建
    *
    */
   @SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
   public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
       RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
       template.setConnectionFactory(connectionFactory);
       //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
       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 14
Source Project: xmfcn-spring-cloud   Source File: RedisConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * springboot2.x 使用LettuceConnectionFactory 代替 RedisConnectionFactory
 * 在application.yml配置基本信息后,springboot2.x  RedisAutoConfiguration能够自动装配 LettuceConnectionFactory 和 RedisConnectionFactory 及其 RedisTemplate
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
    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);
    //使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 15
Source Project: Project   Source File: CartTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void settingKeyAndValueSerializers() {
	// need a local version so we can tweak the serializer
	RedisTemplate<String, Product> redis = new RedisTemplate<String, Product>();
	redis.setConnectionFactory(cf);
	redis.setKeySerializer(new StringRedisSerializer());
	redis.setValueSerializer(new Jackson2JsonRedisSerializer<Product>(Product.class));
	redis.afterPropertiesSet(); // if this were declared as a bean, you wouldn't have to do this
	
	Product product = new Product();
	product.setSku("9781617291203");
	product.setName("Spring in Action");
	product.setPrice(39.99f);
	
	redis.opsForValue().set(product.getSku(), product);
	
	Product found = redis.opsForValue().get(product.getSku());
	assertEquals(product.getSku(), found.getSku());
	assertEquals(product.getName(), found.getName());
	assertEquals(product.getPrice(), found.getPrice(), 0.005);
	
	StringRedisTemplate stringRedis = new StringRedisTemplate(cf);
	String json = stringRedis.opsForValue().get(product.getSku());
	assertEquals("{\"sku\":\"9781617291203\",\"name\":\"Spring in Action\",\"price\":39.99}", json);
}
 
Example 16
Source Project: soul   Source File: RateLimiterPluginDataHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handlerPlugin(final PluginData pluginData) {
    if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
        //init redis
        RateLimiterConfig rateLimiterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), RateLimiterConfig.class);
        //spring data redisTemplate
        if (Objects.isNull(Singleton.INST.get(ReactiveRedisTemplate.class))
                || Objects.isNull(Singleton.INST.get(RateLimiterConfig.class))
                || !rateLimiterConfig.equals(Singleton.INST.get(RateLimiterConfig.class))) {
            LettuceConnectionFactory lettuceConnectionFactory = createLettuceConnectionFactory(rateLimiterConfig);
            lettuceConnectionFactory.afterPropertiesSet();
            RedisSerializer<String> serializer = new StringRedisSerializer();
            RedisSerializationContext<String, String> serializationContext =
                    RedisSerializationContext.<String, String>newSerializationContext().key(serializer).value(serializer).hashKey(serializer).hashValue(serializer).build();
            ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
            Singleton.INST.single(ReactiveRedisTemplate.class, reactiveRedisTemplate);
            Singleton.INST.single(RateLimiterConfig.class, rateLimiterConfig);
        }
    }
}
 
Example 17
Source Project: SpringBootLearn   Source File: commons.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * RedisTemplate
 *
 * @return
 */
@Bean
public RedisTemplate<String, Serializable> limitRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 18
Source Project: FEBS-Security   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> limitRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example 19
Source Project: DouBiNovel   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
    @Bean
    public CacheManager cacheManager() {

        // 设置序列化
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
                // value序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
//                .disableCachingNullValues()
                // 缓存过期时间
                .entryTtl(Duration.ofMinutes(5));

        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(lettuceConnectionFactory)
                .cacheDefaults(config)
                .transactionAware();
        @SuppressWarnings("serial")
        Set<String> cacheNames = new HashSet<String>() {
            {
                add("codeNameCache");
            }
        };
        builder.initialCacheNames(cacheNames);
        return builder.build();
    }
 
Example 20
Source Project: codeway_service   Source File: RedisConfig.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置自定义Json序列化器 jackson2
 *
 * @param redisConnectionFactory :redis连接工厂
 * @return :RedisTemplate
 */
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    // 使用jackson2序列化
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule());
    jackson2JsonRedisSerializer.setObjectMapper(mapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);

    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();


    // 设置默认使用Jackson序列化
    template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
    return template;
}
 
Example 21
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 22
Source Project: x7   Source File: L3RedisCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public L3CacheStorageCustomizer l3CacheStorageCustomizer(){

    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(hostName, port);
    config.setPassword(password);
    config.setDatabase(database);

    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config);
    connectionFactory.afterPropertiesSet();


    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
            Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);

    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(connectionFactory);
    stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
    stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());

    stringRedisTemplate.afterPropertiesSet();

    return () -> {

        L3CacheStorage l3CacheStorage = new DefaultL3CacheStorage();
        ((DefaultL3CacheStorage) l3CacheStorage).setStringRedisTemplate(stringRedisTemplate);

        return l3CacheStorage;
    };
}
 
Example 23
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 24
Source Project: springBoot-study   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * 设置数据存入 redis 的序列化方式,并开启事务
	 * 
	 * @param redisTemplate
	 * @param factory
	 */
	private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
		/*
		 * 设置 序列化器 .
		 * 如果不设置,那么在用实体类(未序列化)进行存储的时候,会提示错误: Failed to serialize object using DefaultSerializer;
		 */
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		// 开启事务
//		redisTemplate.setEnableTransactionSupport(true);
		// 将连接工厂设置到模板类中
		redisTemplate.setConnectionFactory(factory);
	}
 
Example 25
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 26
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 27
Source Project: microservice-recruit   Source File: CacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 28
Source Project: ChengFeng1.5   Source File: RedisConfig.java    License: 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 29
Source Project: MyCommunity   Source File: RedisConfig.java    License: 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 30
Source Project: rqueue   Source File: RedisUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static <V> List<Object> executePipeLine(
    RedisTemplate<String, V> template, RedisPipelineCallback callback) {
  return template.executePipelined(
      (RedisCallback<Object>)
          connection -> {
            RqueueRedisSerializer valueSerializer =
                (RqueueRedisSerializer) template.getValueSerializer();
            StringRedisSerializer keySerializer =
                (StringRedisSerializer) template.getKeySerializer();
            callback.doInRedis(connection, keySerializer, valueSerializer);
            return null;
          });
}