首页  

redis RedisTemplate 和 StringRedisTemplate     所属分类 redis 浏览量 63
public class StringRedisTemplate extends RedisTemplate< String, String> 



@Autowired
private StringRedisTemplate stringRedisTemplate;

@Autowired
private RedisTemplate< String, Serializable> redisCacheTemplate;


public SubscribeStore put(String topic,String clientId,SubscribeStore subscribeStore){
    redisCacheTemplate.opsForHash().put(CACHE_PRE + topic,clientId,subscribeStore);
    // 注意 写入用的 stringRedisTemplate
    stringRedisTemplate.opsForSet().add(CACHE_CLIENT_PRE + clientId,topic);
    return subscribeStore;
}

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {
    @Bean
    public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate< String, Serializable> template = new RedisTemplate< >();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}


DataType type = redisTemplate.type(key);


if(DataType.SET.equals(type)){
    // JSON: Unrecognized token 'topic002': was expecting 'null', 'true', 'false' or NaN
    // Set value = redisTemplate.opsForSet().members(key);
    Set value = stringRedisTemplate.opsForSet().members(key);
}

注意:
写入用 stringRedisTemplate ,读取也要用 stringRedisTemplate

否则反序列化报错 


redis-cli type groza:client:localtest002 set scard groza:client:localtest002 (integer) 3 smembers groza:client:localtest002 1) "topic002" 2) "topic005" 3) "topic006" sadd set001 1 2 3 (integer) 3 127.0.0.1:6379> scard set001 (integer) 3 127.0.0.1:6379> smembers set001 1) "1" 2) "2" 3) "3"
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; RedisSerializer stringSerializer = new StringRedisSerializer(); GenericJackson2JsonRedisSerializer ser = new GenericJackson2JsonRedisSerializer(); String str = "hello"; byte[] bytes = stringSerializer.serialize(str); byte[] bytes2 = ser.serialize(str); // 5 System.out.println(bytes.length); // 7 System.out.println(bytes2.length); // hello System.out.println(new String(bytes)); // "hello" System.out.println(new String(bytes2)); // hello System.out.println(stringSerializer.deserialize(bytes)); // hello System.out.println(ser.deserialize(bytes2)); try { System.out.println(ser.deserialize(bytes)); }catch(Exception e){ // org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized token 'hello': was expecting ('true', 'false' or 'null') System.out.println(e+""); } hello 反序列化失败 org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized token 'hello': was expecting ('true', 'false' or 'null')
https://gitee.com/dyyx/netty-learning-example/blob/master/netty-iot/src/main/java/com/sanshengshui/iot/store/cache/GrozaSubscribeNotWildcardCache.java

上一篇     下一篇
MQTT 入门介绍

MQTT 主题

MQTT 消息类型

MQTT 发展趋势

MQTT协议总结

小学数学学习资料收集