使用RedisTemplate从Redis获取Set值

Zee*_*han 12 java spring redis jedis

我能够从Redis使用中检索值Jedis:

public static void main(String[] args) {
        Jedis jedis = new Jedis(HOST, PORT);
        jedis.connect();
        Set<String> set = jedis.smembers(KEY);
        for (String s : set) {
            System.out.println(s);
        }
        jedis.disconnect();
        jedis.close();
    }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用Spring时RedisTemplate,我没有得到任何数据.我的数据存储RedisSet.

      // inject the actual template 
      @Autowired
      private RedisTemplate<String, Object> template;

      // inject the template as SetOperations
      @Resource(name="redisTemplate")
      private SetOperations<String,String> setOps;

public String logHome() {       
        Set<String> set =  setOps.members(KEY);
        for(String str:set){
            System.out.println(str); //EMPTY
        }       
        Set<byte[]> keys = template.getConnectionFactory().getConnection().keys("*".getBytes());
        Iterator<byte[]> it = keys.iterator();
        while(it.hasNext()){
            byte[] data = (byte[])it.next();
            System.out.println(new String(data, 0, data.length)); //KEYS are printed.
        }
        Set<Object> mySet = template.boundSetOps(KEY).members();        
        System.out.println(mySet); //EMPTY      
        return "";
    }
Run Code Online (Sandbox Code Playgroud)

有人可以指出我错过了什么吗?

编辑:我的RedisTemplate的xml配置.

 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory"/>

     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="myhostname" p:port="6379" />
Run Code Online (Sandbox Code Playgroud)

mp9*_*1de 27

简而言之

您必须配置序列化程序.

说明

Redis模板使用序列化程序来获取键,值和散列键/值.序列化程序用于将Java输入转换为存储在Redis中的表示形式.如果您不配置任何内容,则序列化程序默认为JdkSerializationRedisSerializer.因此,如果您要求keyJava代码中的密钥,则序列化程序会将其转换为

"\xac\xed\x00\x05t\x00\x03key"
Run Code Online (Sandbox Code Playgroud)

和Spring Data Redis使用这些字节作为查询Redis的密钥.

您可以使用Spring Data Redis添加数据并使用以下命令查询redis-cli:

template.boundSetOps("myKey").add(new Date());
Run Code Online (Sandbox Code Playgroud)

然后在 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"
Run Code Online (Sandbox Code Playgroud)

如您所见,String和Date被序列化为一些代表Java序列化对象的疯狂字节.

您的代码建议您要存储基于字符串的键和值.只需设置StringRedisSerializer你的RedisTemplate

Java配置

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
Run Code Online (Sandbox Code Playgroud)

XML配置

<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"/>
Run Code Online (Sandbox Code Playgroud)

运行代码后的输出如下:

value
key
[value]
Run Code Online (Sandbox Code Playgroud)

Spring Data Redis有一些有趣的序列化器,允许在各种系统之间进行消息交换.您可以从内置序列化程序中进行选择

  • JacksonJsonRedisSerializer
  • Jackson2JsonRedisSerializer
  • JdkSerializationRedisSerializer(默认)
  • OxmSerializer
  • GenericToStringSerializer

或创建自己的.

我使用Spring Data Redis 1.5.1.RELEASE和jedis 2.6.2来验证您的问题的结果.HTH,马克

进一步阅读: