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

The following examples show how to use org.springframework.data.redis.core.RedisTemplate#setKeySerializer() . 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: RedisCacheAutoConfiguration.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 重新配置一个RedisTemplate
 *
 * @param factory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(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<String> stringSerializer = new StringRedisSerializer();
    // key采用String的序列化方式
    template.setKeySerializer(stringSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);
    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.setDefaultSerializer(jackson2JsonRedisSerializer);
    return template;
}
 
Example 2
Source File: RedisConfig.java    From spring-boot-study with 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 3
Source File: RedisConfig.java    From springboot_cwenao with MIT License 6 votes vote down vote up
@Bean(name="redisTemplate")
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

    RedisTemplate<String, String> template = new RedisTemplate<>();


    RedisSerializer<String> redisSerializer = new StringRedisSerializer();

    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);

    template.setConnectionFactory(factory);
    //key序列化方式
    template.setKeySerializer(redisSerializer);
    //value序列化
    template.setValueSerializer(jackson2JsonRedisSerializer);
    //value hashmap序列化
    template.setHashValueSerializer(jackson2JsonRedisSerializer);

    return template;
}
 
Example 4
Source File: RedisConfig.java    From RuoYi-Vue with MIT License 6 votes vote down vote up
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);

    FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(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 5
Source File: RedisConfig.java    From biliob_backend with MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);

    // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    Jackson2JsonRedisSerializer<Object> 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 6
Source File: EmailRedisTemplateConfiguration.java    From spring-boot-email-tools with Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("valueTemplate")
public RedisTemplate<String, EmailSchedulingData> createValueTemplate() throws IOException {
    RedisTemplate<String, EmailSchedulingData> template = new RedisTemplate<>();
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jdkSerializationRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(stringSerializer);

    template.setConnectionFactory(redisConnectionFactory);
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}
 
Example 7
Source File: RedisConfig.java    From spring-music with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Album> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Album> template = new RedisTemplate<>();

    template.setConnectionFactory(redisConnectionFactory);

    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    RedisSerializer<Album> albumSerializer = new Jackson2JsonRedisSerializer<>(Album.class);

    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(albumSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(albumSerializer);

    return template;
}
 
Example 8
Source File: J2CacheSpringRedisAutoConfiguration.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
@ConditionalOnBean(name = "j2CahceRedisConnectionFactory")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") JedisConnectionFactory j2CahceRedisConnectionFactory) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(new J2CacheSerializer());
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example 9
Source File: RedisCacheConfig.java    From pig with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new JdkSerializationRedisSerializer());
    return template;
}
 
Example 10
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 11
Source File: J2CacheSpringRedisAutoConfiguration.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") RedisConnectionFactory j2CahceRedisConnectionFactory,
		@Qualifier("j2CacheValueSerializer") RedisSerializer<Object> j2CacheSerializer) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(j2CacheSerializer);
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example 12
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 13
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 14
Source File: CacheConfig.java    From microservice-recruit with 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 15
Source File: CacheConfig.java    From microservice-recruit with 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 16
Source File: RedisConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
@Bean("redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
	RedisTemplate<String, Object> template = new RedisTemplate<>();
	template.setConnectionFactory(factory);
	Jackson2JsonRedisSerializer<Object> 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);
	template.setValueSerializer(jackson2JsonRedisSerializer);
	template.setKeySerializer(stringRedisSerializer());
	template.afterPropertiesSet();
	return template;
}
 
Example 17
Source File: RedisCacheFactory.java    From alcor with Apache License 2.0 5 votes vote down vote up
private <K, V> RedisTemplate<K, V> getRedisTemplate(Class<V> v){

        RedisTemplate<K, V> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());

        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.afterPropertiesSet();

        return template;
    }
 
Example 18
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 19
Source File: AppConfig.java    From Redis-4.x-Cookbook with MIT License 5 votes vote down vote up
@Bean
RedisTemplate<String, User> redisTemplate(){
	RedisTemplate<String, User> template = new RedisTemplate<>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
	template.setConnectionFactory(jedisConnectionFactory());
	return template;
}
 
Example 20
Source File: RedisConfig.java    From ElementVueSpringbootCodeTemplate with Apache License 2.0 4 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
	RedisTemplate template = new RedisTemplate();

	template.setConnectionFactory(factory);

	RedisSerializer keySerializer = new StringRedisSerializer();

	// 设置key序列化类,否则key前面会多了一些乱码
	template.setKeySerializer(keySerializer);
	template.setHashKeySerializer(keySerializer);

	// FIXME 有些版本有bug,没有序列化的只能序列化,但无法反序列化
	GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();

	// 设置内容序列化类
	template.setValueSerializer(jsonSerializer);

	template.afterPropertiesSet();

	return template;
}