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

The following examples show how to use org.springframework.data.redis.core.RedisTemplate#setHashKeySerializer() . 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: 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 3
Source File: CacheManagerBuilder.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private CacheManager decision(org.springframework.cache.CacheManager springCacheManager) {
	if (springCacheManager instanceof EhCacheCacheManager) {
		EhCacheManager ehCacheManager = new EhCacheManager();
		ehCacheManager.setCacheManager(((EhCacheCacheManager) springCacheManager).getCacheManager());
		return ehCacheManager;
	}
	
	if (springCacheManager instanceof org.springframework.data.redis.cache.RedisCacheManager) {
		GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
		RedisTemplate rt = new RedisTemplate<Object, Object>();
		rt.setConnectionFactory(SpringContextUtils.getBean(RedisConnectionFactory.class));
		rt.setKeySerializer(jsonSerializer);
		rt.setHashKeySerializer(jsonSerializer);
		rt.setBeanClassLoader(this.getClass().getClassLoader());
		rt.afterPropertiesSet();
		RedisCacheManager redisCacheManager = new RedisCacheManager();
		redisCacheManager.setRedisTemplate(rt);
		return redisCacheManager;
	}
	return new SpringCacheManager(springCacheManager);
}
 
Example 4
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 5
Source File: RedisConfig.java    From bluemix-cloud-connectors with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Person> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Person> template = new RedisTemplate<>();

    template.setConnectionFactory(redisConnectionFactory);

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

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

    return template;
}
 
Example 6
Source File: RedisConfig.java    From demo-project with 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 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: 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 9
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 10
Source File: RedisConfig.java    From light-reading-cloud with MIT License 5 votes vote down vote up
/** 设置RedisTemplate的序列化方式 */
public void setSerializer(RedisTemplate redisTemplate) {
    StringRedisSerializer stringRedisSerializer = 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);
    //设置键(key)的序列化方式
    redisTemplate.setKeySerializer(stringRedisSerializer);
    //设置值(value)的序列化方式
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
}
 
Example 11
Source File: TcpChannelMsgManage.java    From jt809-tcp-server with MIT License 5 votes vote down vote up
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    RedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(jdkSerializer);
    redisTemplate.afterPropertiesSet();
    this.redisTemplate = redisTemplate;
}
 
Example 12
Source File: RedisConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
/**
 * 设置数据存入 redis 的序列化方式
 * @param redisTemplate
 * @param factory
 */
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    //使用fastjson序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value值的序列化采用fastJsonRedisSerializer
    redisTemplate.setValueSerializer(fastJsonRedisSerializer);
    redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
    // key的序列化采用StringRedisSerializer
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
    log.debug("自定义RedisTemplate加载完成");
}
 
Example 13
Source File: RedisConfiguration.java    From WTFDYUM with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, Event> eventRedisTemplate() {
    final RedisTemplate<String, Event> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(jsonSerializer(Event.class, objectMapper()));
    template.setValueSerializer(jsonSerializer(Event.class, objectMapper()));
    return template;
}
 
Example 14
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 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: 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 17
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 18
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 19
Source File: RedisConfiguration.java    From spring-boot-with-multi-redis with MIT License 4 votes vote down vote up
private void setSerializer(RedisTemplate<String, String> template) {
    template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
}
 
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;
}