Get Set value from Redis using RedisTemplate

前端 未结 2 448
执笔经年
执笔经年 2021-01-31 20:42

I am able to retrieve values from Redis using Jedis:

public static void main(String[] args) {
        Jedis jedis = new Jedis(HOST, POR         


        
相关标签:
2条回答
  • 2021-01-31 21:17

    You could do it much easier with Redisson:

    public static void main(String[] args) {
        Config conf = new Config();
        conf.useSingleServer().setAddress(host + ":" + port);
    
        RedissonClient redisson = Redisson.create(conf);
        RSet<String> set = redisson.getSet(KEY)
        for (String s : set.readAllValues()) {
            System.out.println(s);
        }
        redisson.shutdown();
    }
    

    This framewrok handles serialization and work with connection so you don't need to deal with it each time. Work with Redis as you used to work with Java objects (Set, Map, List ...). It supports many popular codecs too.

    0 讨论(0)
  • 2021-01-31 21:27

    In short

    You have to configure serializers.

    Explanation

    The Redis template uses serializers for keys, values and hash keys/values. Serializers are used to convert the Java input into the representation that is stored within Redis. If you do not configure anything, the serializer defaults to JdkSerializationRedisSerializer. So if you ask for a key key in your Java code, the serializer converts it to

    "\xac\xed\x00\x05t\x00\x03key"
    

    and Spring Data Redis uses those bytes as the key to query Redis.

    You can add data with Spring Data Redis and query it using the redis-cli:

    template.boundSetOps("myKey").add(new Date());
    

    and then in the redis-cli

    127.0.0.1:6379> keys *
    1) "\xac\xed\x00\x05t\x00\x05myKey"
    127.0.0.1:6379> SMEMBERS "\xac\xed\x00\x05t\x00\x05myKey"
    1) "\xac\xed\x00\x05sr\x00\x0ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\b\x00\x00\x01N\xcf#\x9cHx"
    

    As you see, the String and the Date are serialized into some crazy bytes that represent a Java-serialized object.

    Your code suggests you want to store String-based keys and values. Just set the StringRedisSerializer in your RedisTemplate

    Java configuration

    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    

    XML configuration

    <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" 
        p:connection-factory-ref="jedisConnectionFactory">
        <property name="keySerializer" ref="stringSerializer"/>
        <property name="valueSerializer" ref="stringSerializer"/>
    </bean>
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
        p:host-name="myhostname" p:port="6379"/>
    

    The output after running your code looks like then:

    value
    key
    [value]
    

    Spring Data Redis has some interesting serializers that allow message exchange between various systems. You can choose either from the built-in serializers

    • JacksonJsonRedisSerializer
    • Jackson2JsonRedisSerializer
    • JdkSerializationRedisSerializer (default)
    • OxmSerializer
    • GenericToStringSerializer

    or create your own.

    I used Spring Data Redis 1.5.1.RELEASE and jedis 2.6.2 to verify the result of your question. HTH, Mark

    Further read:

    • Spring Data Redis: Serializers
    • Gist containing your example
    0 讨论(0)
提交回复
热议问题